From e5b9b0ccd397eeefb35c0cebde8ef26c8e3ebfd3 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Thu, 16 Feb 2006 19:35:22 +0000 Subject: [PATCH] Added read-only option to non-propagating user transactions as well as TransactionUtil methods git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2420 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../transaction/DummyTransactionService.java | 17 ++++--- .../transaction/TransactionComponent.java | 14 ++++++ .../repo/transaction/TransactionUtil.java | 49 +++++++++++++++++-- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/source/java/org/alfresco/repo/transaction/DummyTransactionService.java b/source/java/org/alfresco/repo/transaction/DummyTransactionService.java index 28b9e263e8..fa21ec0fc3 100644 --- a/source/java/org/alfresco/repo/transaction/DummyTransactionService.java +++ b/source/java/org/alfresco/repo/transaction/DummyTransactionService.java @@ -45,17 +45,22 @@ public class DummyTransactionService implements TransactionService return false; } - public UserTransaction getNonPropagatingUserTransaction() - { - return txn; - } - public UserTransaction getUserTransaction() { return txn; } - public UserTransaction getUserTransaction(boolean readonly) + public UserTransaction getUserTransaction(boolean readOnly) + { + return txn; + } + + public UserTransaction getNonPropagatingUserTransaction() + { + return txn; + } + + public UserTransaction getNonPropagatingUserTransaction(boolean readOnly) { return txn; } diff --git a/source/java/org/alfresco/repo/transaction/TransactionComponent.java b/source/java/org/alfresco/repo/transaction/TransactionComponent.java index 9f4b2ae636..8814849584 100644 --- a/source/java/org/alfresco/repo/transaction/TransactionComponent.java +++ b/source/java/org/alfresco/repo/transaction/TransactionComponent.java @@ -99,4 +99,18 @@ public class TransactionComponent implements TransactionService TransactionDefinition.TIMEOUT_DEFAULT); return txn; } + + /** + * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRES_NEW + */ + public UserTransaction getNonPropagatingUserTransaction(boolean readOnly) + { + SpringAwareUserTransaction txn = new SpringAwareUserTransaction( + transactionManager, + (readOnly | this.readOnly), + TransactionDefinition.ISOLATION_DEFAULT, + TransactionDefinition.PROPAGATION_REQUIRES_NEW, + TransactionDefinition.TIMEOUT_DEFAULT); + return txn; + } } diff --git a/source/java/org/alfresco/repo/transaction/TransactionUtil.java b/source/java/org/alfresco/repo/transaction/TransactionUtil.java index a18ea58457..ea18a0cfce 100644 --- a/source/java/org/alfresco/repo/transaction/TransactionUtil.java +++ b/source/java/org/alfresco/repo/transaction/TransactionUtil.java @@ -71,11 +71,30 @@ public class TransactionUtil TransactionService transactionService, TransactionWork transactionWork) { - return executeInTransaction(transactionService, transactionWork, false); + return executeInTransaction(transactionService, transactionWork, false, false); } /** - * Execute the transaction work in a non propigating user transaction + * Execute the transaction work in a user transaction. + * Any current transaction will be continued. + * + * @param transactionService the transaction service + * @param transactionWork the transaction work + * @param readOnly true if the transaction should be read-only + * + * @throws java.lang.RuntimeException if the transaction was rolled back + */ + public static R executeInUserTransaction( + TransactionService transactionService, + TransactionWork transactionWork, + boolean readOnly) + { + return executeInTransaction(transactionService, transactionWork, false, readOnly); + } + + /** + * Execute the transaction work in a writable, non-propagating user transaction. + * Any current transaction will be suspended a new one started. * * @param transactionService the transaction service * @param transactionWork the transaction work @@ -86,7 +105,25 @@ public class TransactionUtil TransactionService transactionService, TransactionWork transactionWork) { - return executeInTransaction(transactionService, transactionWork, true); + return executeInTransaction(transactionService, transactionWork, true, false); + } + + /** + * Execute the transaction work in a non-propagating user transaction. + * Any current transaction will be suspended a new one started. + * + * @param transactionService the transaction service + * @param transactionWork the transaction work + * @param readOnly true if the transaction should be read-only + * + * @throws java.lang.RuntimeException if the transaction was rolled back + */ + public static R executeInNonPropagatingUserTransaction( + TransactionService transactionService, + TransactionWork transactionWork, + boolean readOnly) + { + return executeInTransaction(transactionService, transactionWork, true, readOnly); } /** @@ -98,13 +135,15 @@ public class TransactionUtil * ignored or re-thrown * @param nonPropagatingUserTransaction indicates whether the transaction * should be non propigating or not + * @param readOnly true if the transaction should be read-only * * @throws java.lang.RuntimeException if the transaction was rolled back */ private static R executeInTransaction( TransactionService transactionService, TransactionWork transactionWork, - boolean nonPropagatingUserTransaction) + boolean nonPropagatingUserTransaction, + boolean readOnly) { ParameterCheck.mandatory("transactionWork", transactionWork); @@ -118,7 +157,7 @@ public class TransactionUtil } else { - txn = transactionService.getUserTransaction(); + txn = transactionService.getUserTransaction(readOnly); } try