deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject git commit: DELTASPIKE-608 @Transactional#readOnly
Date Mon, 26 May 2014 13:09:16 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 98d9422b3 -> e75e2a5e9


DELTASPIKE-608 @Transactional#readOnly


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

Branch: refs/heads/master
Commit: e75e2a5e95ba7bea13f7d6f29a0e30687595d2cb
Parents: 98d9422
Author: gpetracek <gpetracek@apache.org>
Authored: Mon May 26 15:05:19 2014 +0200
Committer: gpetracek <gpetracek@apache.org>
Committed: Mon May 26 15:05:19 2014 +0200

----------------------------------------------------------------------
 .../jpa/api/transaction/Transactional.java      |  5 ++
 .../ResourceLocalTransactionStrategy.java       | 50 ++++++++++++--------
 2 files changed, 35 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e75e2a5e/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
index e993d16..efdeaf7 100644
--- a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
+++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
@@ -62,4 +62,9 @@ public @interface Transactional
      */
     @Nonbinding Class<? extends Annotation>[] qualifier() default Any.class;
 
+    /**
+     * Only evaluated on the first/outermost transactional bean/method in the chain
+     * @return true to trigger #rollback for the current transaction(s), false otherwise
+     */
+    boolean readOnly() default false;
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/e75e2a5e/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
index 26ce151..a844642 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java
@@ -190,33 +190,37 @@ public class ResourceLocalTransactionStrategy implements TransactionStrategy
                         Set<EntityManagerEntry> entityManagerEntryList =
                             transactionBeanStorage.getUsedEntityManagerEntries();
 
-                        boolean rollbackOnly = false;
-                        // but first try to flush all the transactions and write the updates
to the database
-                        for (EntityManagerEntry currentEntityManagerEntry : entityManagerEntryList)
+                        boolean rollbackOnly = isRollbackOnly(transactionalAnnotation);
+
+                        if (!rollbackOnly)
                         {
-                            EntityTransaction transaction = getTransaction(currentEntityManagerEntry);
-                            if (transaction != null && transaction.isActive())
+                            // but first try to flush all the transactions and write the
updates to the database
+                            for (EntityManagerEntry currentEntityManagerEntry : entityManagerEntryList)
                             {
-                                try
+                                EntityTransaction transaction = getTransaction(currentEntityManagerEntry);
+                                if (transaction != null && transaction.isActive())
                                 {
-                                    if (!commitFailed)
+                                    try
                                     {
-                                        currentEntityManagerEntry.getEntityManager().flush();
-
-                                        if (!rollbackOnly && transaction.getRollbackOnly())
+                                        if (!commitFailed)
                                         {
-                                            // don't set commitFailed to true directly
-                                            // (the order of the entity-managers isn't deterministic
-                                            //  -> tests would break)
-                                            rollbackOnly = true;
+                                            currentEntityManagerEntry.getEntityManager().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)
-                                {
-                                    firstException = e;
-                                    commitFailed = true;
-                                    break;
+                                    catch (Exception e)
+                                    {
+                                        firstException = e;
+                                        commitFailed = true;
+                                        break;
+                                    }
                                 }
                             }
                         }
@@ -267,6 +271,12 @@ public class ResourceLocalTransactionStrategy implements TransactionStrategy
         }
     }
 
+    //allows to use a custom tx-controller in a custom strategy
+    protected boolean isRollbackOnly(Transactional transactionalAnnotation)
+    {
+        return transactionalAnnotation != null && transactionalAnnotation.readOnly();
+    }
+
     private void rollbackAllTransactions(Set<EntityManagerEntry> entityManagerEntryList)
     {
         for (EntityManagerEntry currentEntityManagerEntry : entityManagerEntryList)


Mime
View raw message