openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1536595 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/ openjpa-persistence-jdbc/src/test/resources/META-INF/
Date Tue, 29 Oct 2013 06:36:34 GMT
Author: struberg
Date: Tue Oct 29 06:36:34 2013
New Revision: 1536595

URL: http://svn.apache.org/r1536595
Log:
OPENJPA-2437 transactional listeners added too late to observe begin event

txs 2 rmannibucau for the fix.
Patch applied with cleanup and tests pimped. 


Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java?rev=1536595&r1=1536594&r2=1536595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java
Tue Oct 29 06:36:34 2013
@@ -227,8 +227,6 @@ public abstract class AbstractBrokerFact
         DelegatingStoreManager dsm = createDelegatingStoreManager();
 
         ((BrokerImpl) broker).initialize(this, dsm, managed, connRetainMode, fromDeserialization);
-        if (!fromDeserialization)
-            addListeners(broker);
 
         // if we're using remote events, register the event manager so
         // that it can broadcast commit notifications from the broker

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1536595&r1=1536594&r2=1536595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java Tue
Oct 29 06:36:34 2013
@@ -391,6 +391,11 @@ public class BrokerImpl implements Broke
         _printParameters =
             Boolean.parseBoolean(Configurations.parseProperties(_conf.getConnectionFactoryProperties()).getProperty(
                 PRINT_PARAMETERS_CONFIG_STR, "false"));
+
+        // do it before begin event otherwise transactional listeners can't use it, see @Auditable
+        if (!fromDeserialization)
+            _factory.addListeners(this);
+
         // synch with the global transaction in progress, if any
         if (_factory.syncWithManagedTransaction(this, false))
             beginInternal();

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java?rev=1536595&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
Tue Oct 29 06:36:34 2013
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.audit;
+
+import junit.framework.TestCase;
+import org.apache.openjpa.ee.ManagedRuntime;
+import org.apache.openjpa.kernel.Audited;
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.lib.conf.Configuration;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import java.util.Collection;
+
+public class TestBeginEventOnTransactionListener extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(X.class, AuditedEntry.class, CLEAR_TABLES);
+    }
+
+    @Override
+    protected String getPersistenceUnitName() {
+        return "auditjta";
+    }
+
+
+    public void test() throws Exception {
+        doTest(emf);
+        assertTrue(MockAuditor.called);
+    }
+
+    private void doTest(final EntityManagerFactory emf) throws Exception {
+        final ManagedRuntime runtime = OpenJPAEntityManagerFactorySPI.class.cast(emf)
+                .getConfiguration().getManagedRuntimeInstance();
+
+        runtime.getTransactionManager().begin();
+        try {
+            final EntityManager em = emf.createEntityManager();
+            em.joinTransaction();
+
+            final X x = new X();
+            em.persist(x);
+            runtime.getTransactionManager().commit();
+        }
+        finally {
+            emf.close();
+        }
+    }
+
+
+    public static class MockAuditor implements Auditor
+    {
+        public static boolean called = false;
+
+        @Override
+        public void audit(Broker broker, Collection<Audited> newObjects,
+                          Collection<Audited> updates, Collection<Audited> deletes)
{
+            called = true;
+        }
+
+        @Override
+        public boolean isRollbackOnError() {
+            return false;
+        }
+
+        @Override
+        public void close() throws Exception {
+
+        }
+
+        @Override
+        public void setConfiguration(Configuration conf) {
+
+        }
+
+        @Override
+        public void startConfiguration() {
+
+        }
+
+        @Override
+        public void endConfiguration() {
+
+        }
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/audit/TestBeginEventOnTransactionListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml?rev=1536595&r1=1536594&r2=1536595&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml Tue
Oct 29 06:36:34 2013
@@ -442,6 +442,17 @@
     </persistence-unit>
 
 
+  <persistence-unit name="auditjta" transaction-type="JTA">
+    <class>org.apache.openjpa.audit.X</class>
+    <class>org.apache.openjpa.audit.AuditedEntry</class>
+
+    <properties>
+      <property name="openjpa.Auditor" value="org.apache.openjpa.audit.TestBeginEventOnTransactionListener$MockAuditor"
/>
+      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"
/>
+      <property name="openjpa.DynamicEnhancementAgent" value="false" />
+      <property name="openjpa.ManagedRuntime" value="org.apache.openjpa.jta.JTAManagedRuntime"/>
+    </properties>
+  </persistence-unit>
 	<persistence-unit name="audit">
 		<class>org.apache.openjpa.audit.X</class>
 		<class>org.apache.openjpa.audit.AuditedEntry</class>



Mime
View raw message