(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 21 Jul 2010 08:10:02 +0000 (08:10 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 21 Jul 2010 08:10:02 +0000 (08:10 +0000)
test/enterprise/src/main/java/org/wamblee/test/persistence/JpaBuilder.java

index dbc88dc0ad19e6559ace9ae8b094834fe6710a00..dac2789b942e8f5a79442a6b5fb80e37d7bd8fe9 100644 (file)
@@ -66,8 +66,8 @@ public class JpaBuilder implements TransactionResource<EntityManager> {
     private String user;
     private String password;
     private EntityManagerFactory factory;
-    private ThreadSpecificProxyFactory<EntityManager> entityManager; 
-    
+    private ThreadSpecificProxyFactory<EntityManager> entityManager;
+
     /**
      * Constructs the builder.
      * 
@@ -86,7 +86,8 @@ public class JpaBuilder implements TransactionResource<EntityManager> {
         url = aUrl;
         user = aUser;
         password = aPassword;
-        entityManager = new ThreadSpecificProxyFactory<EntityManager>(EntityManager.class);
+        entityManager = new ThreadSpecificProxyFactory<EntityManager>(
+            EntityManager.class);
     }
 
     /**
@@ -163,7 +164,7 @@ public class JpaBuilder implements TransactionResource<EntityManager> {
             LOGGER.log(Level.WARNING, "Exception occured", e);
             rollback(em);
             throw e;
-        } 
+        }
     }
 
     @Override
@@ -188,19 +189,25 @@ public class JpaBuilder implements TransactionResource<EntityManager> {
     @Override
     public void rollback(EntityManager aEntityManager) {
         try {
-            aEntityManager.getTransaction().rollback();
+            EntityTransaction transaction = aEntityManager.getTransaction();
+            if (transaction.isActive()) {
+                transaction.rollback();
+            }
         } finally {
-            aEntityManager.close();
+            if (aEntityManager.isOpen()) {
+                aEntityManager.close();
+            }
             entityManager.set(null);
         }
     }
-    
+
     /**
-     * Gets a contextual reference to an entity manager that delegates to the 
-     * appropriate (current) one which is active for the current transaction. 
-     * @return EntityManager. 
+     * Gets a contextual reference to an entity manager that delegates to the
+     * appropriate (current) one which is active for the current transaction.
+     * 
+     * @return EntityManager.
      */
-    public EntityManager getContextualEntityManager() { 
-        return entityManager.getProxy(); 
+    public EntityManager getContextualEntityManager() {
+        return entityManager.getProxy();
     }
 }