You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2015/03/26 12:41:57 UTC

tomee git commit: OPENEJB-2112 if unwrap was called on the query don't recreate the query

Repository: tomee
Updated Branches:
  refs/heads/master 849292df6 -> bf22e8980


OPENEJB-2112 if unwrap was called on the query don't recreate the query


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/bf22e898
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/bf22e898
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/bf22e898

Branch: refs/heads/master
Commit: bf22e8980bf8430cc7fbe0a83f9a4e84a3269699
Parents: 849292d
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Thu Mar 26 12:41:48 2015 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Thu Mar 26 12:41:48 2015 +0100

----------------------------------------------------------------------
 .../apache/openejb/persistence/JtaQuery.java    | 13 ++++-
 .../openejb/persistence/JtaQueryTest.java       | 56 ++++++++++++++++++++
 2 files changed, 67 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/bf22e898/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaQuery.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaQuery.java b/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaQuery.java
index 6eea5c3..2336f45 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaQuery.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaQuery.java
@@ -44,6 +44,7 @@ public class JtaQuery implements Query {
     private final Collection<QueryOperation> appliedOperations = new ArrayList<QueryOperation>();
 
     private boolean underTx;
+    private boolean unwrap;
     private Query query;
 
     public JtaQuery(final EntityManager entityManager, final JtaEntityManager jtaEntityManager, final Method method, final Object... args) {
@@ -55,13 +56,16 @@ public class JtaQuery implements Query {
         createQuery();
     }
 
-    private void createQuery() {
-        query = jtaEntityManager.createQuery(queryType(), entityManager, method, args);
+    private Query createQuery() {
+        if (!unwrap) {
+            query = jtaEntityManager.createQuery(queryType(), entityManager, method, args);
+        }
         if (!underTx) {
             for (final QueryOperation op : appliedOperations) {
                 query = op.apply(query);
             }
         }
+        return query;
     }
 
     protected Class<? extends Query> queryType() {
@@ -397,6 +401,11 @@ public class JtaQuery implements Query {
      * @see javax.persistence.Query#unwrap(java.lang.Class)
      */
     public <T> T unwrap(final Class<T> cls) {
+        unwrap = true;
+
+        if (getClass() == cls) {
+            return cls.cast(this);
+        }
         return query.unwrap(cls);
     }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/bf22e898/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
new file mode 100644
index 0000000..c286225
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/persistence/JtaQueryTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.openejb.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.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(ApplicationComposer.class)
+public class JtaQueryTest {
+    @Module
+    public PersistenceUnit unit() {
+        final PersistenceUnit persistenceUnit = new PersistenceUnit();
+        persistenceUnit.setName("test");
+        persistenceUnit.setExcludeUnlistedClasses(true);
+        return persistenceUnit;
+    }
+
+    @PersistenceContext
+    private EntityManager em;
+
+    @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 JtaQuery q = query.unwrap(JtaQuery.class);
+            assertNotNull(Reflections.get(q, "entityManager"));
+            query.getResultList();
+            assertFalse(EntityManager.class.cast(Reflections.get(q, "entityManager")).isOpen());
+        }
+    }
+}