Repository: tomee Updated Branches: refs/heads/master bf22e8980 -> 547bd0dab
openejb.jpa.query.wrap-no-tx flag to control wrapping or not of not jta queries from jta entity manager Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/547bd0da Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/547bd0da Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/547bd0da Branch: refs/heads/master Commit: 547bd0dab405d74aaee2a47e0830aac9f25d8727 Parents: bf22e89 Author: Romain Manni-Bucau <[email protected]> Authored: Thu Mar 26 14:00:48 2015 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Thu Mar 26 14:00:48 2015 +0100 ---------------------------------------------------------------------- .../openejb/persistence/JtaEntityManager.java | 9 +++- .../openejb/persistence/JtaQueryTest.java | 45 ++++++++++++++++---- 2 files changed, 44 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/547bd0da/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java b/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java index 5151885..1dfb8b8 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java @@ -18,6 +18,7 @@ package org.apache.openejb.persistence; import org.apache.openejb.OpenEJBRuntimeException; +import org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory; import org.apache.openejb.core.ivm.IntraVmArtifact; import org.apache.openejb.util.LogCategory; import org.apache.openejb.util.Logger; @@ -72,6 +73,7 @@ public class JtaEntityManager implements EntityManager, Serializable { private final boolean extended; private final String unitName; private final Logger logger; + private final boolean wrapNoTxQueries; public JtaEntityManager(final JtaEntityManagerRegistry registry, final EntityManagerFactory entityManagerFactory, final Map properties, final String unitName) { this(unitName, registry, entityManagerFactory, properties, false); @@ -90,6 +92,9 @@ public class JtaEntityManager implements EntityManager, Serializable { this.properties = properties; this.extended = extended; logger = unitName == null ? baseLogger : baseLogger.getChildLogger(unitName); + final String wrapConfig = ReloadableEntityManagerFactory.class.isInstance(entityManagerFactory) ? + ReloadableEntityManagerFactory.class.cast(entityManagerFactory).getUnitProperties().getProperty("openejb.jpa.query.wrap-no-tx", "true") : "true"; + this.wrapNoTxQueries = wrapConfig == null || "true".equalsIgnoreCase(wrapConfig); } EntityManager getEntityManager() { @@ -320,14 +325,14 @@ public class JtaEntityManager implements EntityManager, Serializable { } private Query proxyIfNoTx(final Method method, final Object... args) { - if (!extended && !isTransactionActive()) { + if (wrapNoTxQueries && !extended && !isTransactionActive()) { return new JtaQuery(getEntityManager(), this, method, args); } return createQuery(Query.class, getEntityManager(), method, args); } private <T> TypedQuery<T> typedProxyIfNoTx(final Method method, final Object... args) { - if (!extended && !isTransactionActive()) { + if (wrapNoTxQueries && !extended && !isTransactionActive()) { return new JtaTypedQuery<T>(getEntityManager(), this, method, args); } return createQuery(TypedQuery.class, getEntityManager(), method, args); http://git-wip-us.apache.org/repos/asf/tomee/blob/547bd0da/container/openejb-core/src/test/java/org/apache/openejb/persistence/JtaQueryTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/persistence/JtaQueryTest.java b/container/openejb-core/src/test/java/org/apache/openejb/persistence/JtaQueryTest.java index c286225..0706aa2 100644 --- a/container/openejb-core/src/test/java/org/apache/openejb/persistence/JtaQueryTest.java +++ b/container/openejb-core/src/test/java/org/apache/openejb/persistence/JtaQueryTest.java @@ -16,10 +16,12 @@ */ package org.apache.openejb.persistence; +import org.apache.openejb.jee.jpa.unit.Persistence; import org.apache.openejb.jee.jpa.unit.PersistenceUnit; import org.apache.openejb.junit.ApplicationComposer; import org.apache.openejb.testing.Module; import org.apache.openejb.util.reflection.Reflections; +import org.apache.openjpa.persistence.QueryImpl; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,28 +31,55 @@ import javax.persistence.Query; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; @RunWith(ApplicationComposer.class) public class JtaQueryTest { @Module - public PersistenceUnit unit() { - final PersistenceUnit persistenceUnit = new PersistenceUnit(); - persistenceUnit.setName("test"); - persistenceUnit.setExcludeUnlistedClasses(true); - return persistenceUnit; + public Persistence unit() { + final Persistence persistence = new Persistence(); + { + final PersistenceUnit persistenceUnit = new PersistenceUnit(); + persistenceUnit.setName("testWrapped"); + persistenceUnit.setExcludeUnlistedClasses(true); + persistence.getPersistenceUnit().add(persistenceUnit); + } + { + final PersistenceUnit persistenceUnit = new PersistenceUnit(); + persistenceUnit.setName("testNotWrapped"); + persistenceUnit.setExcludeUnlistedClasses(true); + persistenceUnit.setProperty("openejb.jpa.query.wrap-no-tx", "false"); + persistence.getPersistenceUnit().add(persistenceUnit); + } + return persistence; } - @PersistenceContext - private EntityManager em; + @PersistenceContext(unitName = "testWrapped") + private EntityManager wrapped; + + @PersistenceContext(unitName = "testNotWrapped") + private EntityManager notWrapped; @Test public void jtaUnwrap() { for (int i = 0; i < 2; i++) { // no exception already closed - final Query query = em.createNativeQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS"); + final Query query = wrapped.createNativeQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS"); + assertTrue(query instanceof JtaQuery); final JtaQuery q = query.unwrap(JtaQuery.class); assertNotNull(Reflections.get(q, "entityManager")); query.getResultList(); assertFalse(EntityManager.class.cast(Reflections.get(q, "entityManager")).isOpen()); } } + + @Test + public void raw() { + for (int i = 0; i < 2; i++) { // no exception already closed + final Query query = notWrapped.createNativeQuery("select 1 from INFORMATION_SCHEMA.SYSTEM_USERS"); + assertTrue(query instanceof QueryImpl); + query.getResultList(); + // actually true, that is why we don't have it by default: it leaks + // assertFalse(EntityManager.class.cast(Reflections.get(query, "_em")).isOpen()); + } + } }
