You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/07/29 17:12:14 UTC

[5/6] git commit: DELTASPIKE-248 rollback if EntityTransaction#getRollbackOnly returns true

DELTASPIKE-248 rollback if EntityTransaction#getRollbackOnly returns true


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

Branch: refs/heads/master
Commit: 096385207f654591ff0d74feea8fe4b79b41992a
Parents: c627f70
Author: gpetracek <gp...@apache.org>
Authored: Fri Jul 27 16:54:50 2012 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Sun Jul 29 13:55:21 2012 +0200

----------------------------------------------------------------------
 .../ResourceLocalPersistenceStrategy.java          |   21 ++++++++++++---
 .../test/jpa/api/shared/TestEntityTransaction.java |    4 +-
 2 files changed, 19 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/09638520/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalPersistenceStrategy.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalPersistenceStrategy.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalPersistenceStrategy.java
index 10a34e8..0146187 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalPersistenceStrategy.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalPersistenceStrategy.java
@@ -177,6 +177,8 @@ public class ResourceLocalPersistenceStrategy implements PersistenceStrategy
                 if (firstException == null)
                 {
                     HashMap<Class, EntityManager> emsEntries = transactionBeanStorage.getUsedEntityManagers();
+
+                    boolean rollbackOnly = false;
                     // but first try to flush all the transactions and write the updates to the database
                     for (EntityManager em: emsEntries.values())
                     {
@@ -188,6 +190,13 @@ public class ResourceLocalPersistenceStrategy implements PersistenceStrategy
                                 if (!commitFailed)
                                 {
                                     em.flush();
+
+                                    if (!rollbackOnly && transaction.getRollbackOnly())
+                                    {
+                                        //don't set commitFailed to true directly
+                                        //(the order of the entity-managers isn't deterministic -> tests would break)
+                                        rollbackOnly = true;
+                                    }
                                 }
                             }
                             catch (Exception e)
@@ -198,6 +207,10 @@ public class ResourceLocalPersistenceStrategy implements PersistenceStrategy
                             }
                         }
                     }
+                    if (rollbackOnly)
+                    {
+                        commitFailed = true;
+                    }
 
                     // and now either commit or rollback all transactions
                     for (EntityManager em : emsEntries.values())
@@ -207,13 +220,13 @@ public class ResourceLocalPersistenceStrategy implements PersistenceStrategy
                         {
                             try
                             {
-                                if (!commitFailed)
+                                if (commitFailed || transaction.getRollbackOnly() /*last chance to check it (again)*/)
                                 {
-                                    transaction.commit();
+                                    transaction.rollback();
                                 }
                                 else
                                 {
-                                    transaction.rollback();
+                                    transaction.commit();
                                 }
                             }
                             catch (Exception e)
@@ -230,7 +243,7 @@ public class ResourceLocalPersistenceStrategy implements PersistenceStrategy
 
             transactionBeanStorage.decrementRefCounter();
 
-            if (commitFailed)
+            if (commitFailed && firstException != null /*null if just #getRollbackOnly is true*/)
             {
                 //noinspection ThrowFromFinallyBlock
                 throw firstException;

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/09638520/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/shared/TestEntityTransaction.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/shared/TestEntityTransaction.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/shared/TestEntityTransaction.java
index 056fb0c..dc1eb21 100644
--- a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/shared/TestEntityTransaction.java
+++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/shared/TestEntityTransaction.java
@@ -52,13 +52,13 @@ public class TestEntityTransaction implements EntityTransaction
     @Override
     public void setRollbackOnly()
     {
-        throw new IllegalStateException("not implemented");
+        this.rolledBack = true;
     }
 
     @Override
     public boolean getRollbackOnly()
     {
-        throw new IllegalStateException("not implemented");
+        return this.rolledBack;
     }
 
     @Override