private String user;
     private String password;
     private EntityManagerFactory factory;
-    private ThreadSpecificProxyFactory<EntityManager> entityManager; 
-    
+    private ThreadSpecificProxyFactory<EntityManager> entityManager;
+
     /**
      * Constructs the builder.
      * 
         url = aUrl;
         user = aUser;
         password = aPassword;
-        entityManager = new ThreadSpecificProxyFactory<EntityManager>(EntityManager.class);
+        entityManager = new ThreadSpecificProxyFactory<EntityManager>(
+            EntityManager.class);
     }
 
     /**
             LOGGER.log(Level.WARNING, "Exception occured", e);
             rollback(em);
             throw e;
-        } 
+        }
     }
 
     @Override
     @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();
     }
 }