Author: rmannibucau
Date: Wed Aug 10 11:53:03 2011
New Revision: 1156118

URL: http://svn.apache.org/viewvc?rev=1156118&view=rev
Log:
modifying JTAEntityManager close method to throw an IllegalStateException if 
called from a container managed context or to delegate the close in a bean 
managed context

Modified:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java?rev=1156118&r1=1156117&r2=1156118&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/persistence/JtaEntityManager.java
 Wed Aug 10 11:53:03 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.openejb.persistence;
 
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.core.ThreadContext;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 
@@ -238,10 +240,7 @@ public class JtaEntityManager implements
     public boolean contains(Object entity) {
         final Timer timer = Op.contains.start(this);
         try {
-            if (!extended &&  !isTransactionActive()) {
-                return false;
-            }
-            return getEntityManager().contains(entity);
+            return !(!extended && !isTransactionActive()) && 
getEntityManager().contains(entity);
         } finally {
             timer.stop();
         }
@@ -322,10 +321,29 @@ public class JtaEntityManager implements
         }
     }
 
+    public boolean isContainerManaged() {
+        ThreadContext threadContext = ThreadContext.getThreadContext();
+        if (threadContext == null) {
+            return false;
+        }
+
+        BeanContext di = threadContext.getBeanContext();
+        return di != null && di.isBeanManagedTransaction();
+    }
+
     public void close() {
         if (logger.isDebugEnabled()) {
             logger.debug("PersistenceUnit(name=" + unitName + ") - 
entityManager.close() call ignored - not applicable to a JTA Managed 
EntityManager",  new Exception().fillInStackTrace());
         }
+
+        if (isContainerManaged()) {
+            // with OpenJPA or ElcipseLink or Hibernate
+            // if a method is closed after close() invocation
+            // it throws an IllegalStateException which is spec compliant
+            getEntityManager().close();
+        } else {
+            throw new IllegalStateException("PersistenceUnit(name=" + unitName 
+ ") - entityManager.close() call - See JPA 2.0 section 7.9.1", new 
Exception().fillInStackTrace());
+        }
     }
 
     public boolean isOpen() {


Reply via email to