ensure we dont join transaction if not expected for SynchronizationType.UNSYNCHRONIZED
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/34c4cc7a Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/34c4cc7a Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/34c4cc7a Branch: refs/heads/tomee-7.0.0-M1 Commit: 34c4cc7a2a966414ffc371c6dafab548ea7f224c Parents: 7b1983c Author: Romain Manni-Bucau <[email protected]> Authored: Mon Oct 26 12:09:27 2015 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Mon Oct 26 12:09:27 2015 +0100 ---------------------------------------------------------------------- .../openejb/core/managed/ManagedContainer.java | 2 +- .../core/stateful/StatefulContainer.java | 2 +- .../persistence/JtaEntityManagerRegistry.java | 20 ++++++++++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/34c4cc7a/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java b/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java index b0bbfc6..90e0527 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java @@ -800,7 +800,7 @@ public class ManagedContainer implements RpcContainer { } else { entityManager = entityManagerFactory.createEntityManager(); } - entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager); + entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager, synchronizationType != SynchronizationType.UNSYNCHRONIZED); } else { entityManagerTracker.incCounter(); } http://git-wip-us.apache.org/repos/asf/tomee/blob/34c4cc7a/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java b/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java index 7a5f6ee..e1f58f8 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java @@ -913,7 +913,7 @@ public class StatefulContainer implements RpcContainer { } else { entityManager = entityManagerFactory.createEntityManager(); } - entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager); + entityManagerTracker = new JtaEntityManagerRegistry.EntityManagerTracker(entityManager, synchronizationType != SynchronizationType.UNSYNCHRONIZED); } else { entityManagerTracker.incCounter(); } http://git-wip-us.apache.org/repos/asf/tomee/blob/34c4cc7a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManagerRegistry.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManagerRegistry.java b/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManagerRegistry.java index 082303b..1a0a0e7 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManagerRegistry.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManagerRegistry.java @@ -109,7 +109,7 @@ public class JtaEntityManagerRegistry { // if transaction is active, we need to register the entity manager with the transaction manager if (transactionActive) { - if (synchronizationType != SynchronizationType.UNSYNCHRONIZED) { + if (entityManagerTracker.autoJoinTx) { entityManager.joinTransaction(); } transactionRegistry.putResource(txKey, entityManager); @@ -215,7 +215,8 @@ public class JtaEntityManagerRegistry { if (isTransactionActive()) { for (final Map.Entry<EntityManagerFactory, EntityManagerTracker> entry : entityManagers.entrySet()) { final EntityManagerFactory entityManagerFactory = entry.getKey(); - final EntityManager entityManager = entry.getValue().getEntityManager(); + final EntityManagerTracker tracker = entry.getValue(); + final EntityManager entityManager = tracker.getEntityManager(); final EntityManagerTxKey txKey = new EntityManagerTxKey(entityManagerFactory); final EntityManager oldEntityManager = (EntityManager) transactionRegistry.getResource(txKey); if (entityManager == oldEntityManager) { @@ -225,7 +226,9 @@ public class JtaEntityManagerRegistry { throw new EntityManagerAlreadyRegisteredException("Another entity manager is already registered for this persistence unit"); } - entityManager.joinTransaction(); + if (tracker.autoJoinTx) { + entityManager.joinTransaction(); + } transactionRegistry.putResource(txKey, entityManager); } } @@ -269,8 +272,11 @@ public class JtaEntityManagerRegistry { for (final Map.Entry<EntityManagerFactory, EntityManagerTracker> entry : entityManagers.entrySet()) { final EntityManagerFactory entityManagerFactory = entry.getKey(); - final EntityManager entityManager = entry.getValue().getEntityManager(); - entityManager.joinTransaction(); + final EntityManagerTracker value = entry.getValue(); + final EntityManager entityManager = value.getEntityManager(); + if (value.autoJoinTx) { + entityManager.joinTransaction(); + } final EntityManagerTxKey txKey = new EntityManagerTxKey(entityManagerFactory); transactionRegistry.putResource(txKey, entityManager); } @@ -323,14 +329,16 @@ public class JtaEntityManagerRegistry { // must take care of the first inheritance level private transient int counter; private final EntityManager entityManager; + private final boolean autoJoinTx; - public EntityManagerTracker(final EntityManager entityManager) { + public EntityManagerTracker(final EntityManager entityManager, final boolean autoJoinTx) { if (entityManager == null) { throw new NullPointerException("entityManager is null."); } this.counter = 0; this.entityManager = entityManager; + this.autoJoinTx = autoJoinTx; } public int incCounter() {
