OPENJPA-2437 transactional listeners added too late to observe begin event

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



git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1536595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Struberg 2013-10-29 06:36:34 +00:00
parent 310be13508
commit f4b2428008
4 changed files with 122 additions and 2 deletions

View File

@ -227,8 +227,6 @@ public abstract class AbstractBrokerFactory
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

View File

@ -391,6 +391,11 @@ public class BrokerImpl implements Broker, FindCallbacks, Cloneable, Serializabl
_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();

View File

@ -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() {
}
}
}

View File

@ -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>