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
This commit is contained in:
Derek Hulley 2006-02-16 19:35:22 +00:00
parent 2928954e0a
commit e5b9b0ccd3
3 changed files with 69 additions and 11 deletions

View File

@ -45,17 +45,22 @@ public class DummyTransactionService implements TransactionService
return false; return false;
} }
public UserTransaction getNonPropagatingUserTransaction()
{
return txn;
}
public UserTransaction getUserTransaction() public UserTransaction getUserTransaction()
{ {
return txn; 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; return txn;
} }

View File

@ -99,4 +99,18 @@ public class TransactionComponent implements TransactionService
TransactionDefinition.TIMEOUT_DEFAULT); TransactionDefinition.TIMEOUT_DEFAULT);
return txn; 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;
}
} }

View File

@ -71,11 +71,30 @@ public class TransactionUtil
TransactionService transactionService, TransactionService transactionService,
TransactionWork<R> transactionWork) TransactionWork<R> 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> R executeInUserTransaction(
TransactionService transactionService,
TransactionWork<R> transactionWork,
boolean readOnly)
{
return executeInTransaction(transactionService, transactionWork, false, readOnly);
}
/**
* Execute the transaction work in a <b>writable</b>, non-propagating user transaction.
* Any current transaction will be suspended a new one started.
* *
* @param transactionService the transaction service * @param transactionService the transaction service
* @param transactionWork the transaction work * @param transactionWork the transaction work
@ -86,7 +105,25 @@ public class TransactionUtil
TransactionService transactionService, TransactionService transactionService,
TransactionWork<R> transactionWork) TransactionWork<R> 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> R executeInNonPropagatingUserTransaction(
TransactionService transactionService,
TransactionWork<R> transactionWork,
boolean readOnly)
{
return executeInTransaction(transactionService, transactionWork, true, readOnly);
} }
/** /**
@ -98,13 +135,15 @@ public class TransactionUtil
* ignored or re-thrown * ignored or re-thrown
* @param nonPropagatingUserTransaction indicates whether the transaction * @param nonPropagatingUserTransaction indicates whether the transaction
* should be non propigating or not * 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 * @throws java.lang.RuntimeException if the transaction was rolled back
*/ */
private static <R> R executeInTransaction( private static <R> R executeInTransaction(
TransactionService transactionService, TransactionService transactionService,
TransactionWork<R> transactionWork, TransactionWork<R> transactionWork,
boolean nonPropagatingUserTransaction) boolean nonPropagatingUserTransaction,
boolean readOnly)
{ {
ParameterCheck.mandatory("transactionWork", transactionWork); ParameterCheck.mandatory("transactionWork", transactionWork);
@ -118,7 +157,7 @@ public class TransactionUtil
} }
else else
{ {
txn = transactionService.getUserTransaction(); txn = transactionService.getUserTransaction(readOnly);
} }
try try