Class TransactionUtil
get(UserTransaction, ThrowingSupplier)
, run(UserTransaction, Callable)
, or run(UserTransaction, ThrowingRunnable)
it is not necessary to using complicated try-catch constructions making sure a transaction is orderly completed.
Also, these methods support nesting of invocations.
In that case, only the outer-most invocation will complete the transaction.
Leaving any invocation (nested or not) will set the current transaction to rollback only. That is also the preferred way of forcing a rollback of a transaction.
Invoking an explicit commit or rollback on the user transaction while in a call to any of the utility methods
above will normally unassociate a given transaction from the thread and lead to exceptions when leaving the TransactionUtil
methods. In other words: Do not use the UserTransaction
other than checking on transaction status.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
The TransactionUtilException is used to wrap system style exceptions possibly occurring when invoking transaction management methods, but normally not of interest to application, when invokingget(UserTransaction, ThrowingSupplier)
,run(UserTransaction, Callable)
,run(UserTransaction, ThrowingRunnable)
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V, E extends Exception>
Vget(javax.transaction.UserTransaction ut, ThrowingSupplier<V,E> supplier)
Use this method to execute work in a transaction that returns a result.static void
mandatory(javax.transaction.UserTransaction ut)
Require an outer active transaction.static void
never(javax.transaction.UserTransaction ut)
Do not allow an outer active transaction.static <E extends Exception>
voidrun(javax.transaction.UserTransaction ut, ThrowingRunnable<E> runnable)
Use this method to execute work in a transaction that returns no result.static <V> V
Use this method to execute work in a transaction.
-
Constructor Details
-
TransactionUtil
public TransactionUtil()
-
-
Method Details
-
never
public static void never(javax.transaction.UserTransaction ut)Do not allow an outer active transaction. -
mandatory
public static void mandatory(javax.transaction.UserTransaction ut)Require an outer active transaction. -
run
public static <V> V run(javax.transaction.UserTransaction ut, Callable<V> callable) throws ExceptionUse this method to execute work in a transaction. This is just a utility that helps implementing for example Transaction Servlet Filters that begin and end transactions at the request boundaries.If there is an ongoing transaction already, this method will not begin a new one. Any exception caught in that case will set that new transaction to rollback.
See also
ThreadUtil.confine(Callable, Class)
on how to handle checked exception cases gracefully. -
get
public static <V, E extends Exception> V get(javax.transaction.UserTransaction ut, ThrowingSupplier<V,E> supplier) throws E, TransactionUtil.TransactionUtilExceptionUse this method to execute work in a transaction that returns a result.If there is an ongoing transaction already, this method will not begin a new one. Any exception caught in that case will set that new transaction to rollback.
This method relies on
ThrowingSupplier
to give the caller control over what exceptions may be propagated.Any other checked exceptions that may be thrown by underlying transaction management methods, such as
SystemException
,NotSupportedException
will be wrapped into aTransactionUtil.TransactionUtilException
.- Parameters:
ut
- theUserTransaction
instance of the transaction management system in placesupplier
- TheThrowingSupplier
implementation that defines the work.- Returns:
- The result of the supplier invocation.
- Throws:
Exception
TransactionUtil.TransactionUtilException
-
run
public static <E extends Exception> void run(javax.transaction.UserTransaction ut, ThrowingRunnable<E> runnable) throws E, TransactionUtil.TransactionUtilExceptionUse this method to execute work in a transaction that returns no result.If there is an ongoing transaction already, this method will not begin a new one. Any exception caught in that case will set that new transaction to rollback.
This method relies on
ThrowingSupplier
to give the caller control over what exceptions may be propagated.Any other checked exceptions that may be thrown by underlying transaction management methods, such as
SystemException
,NotSupportedException
will be wrapped into aTransactionUtil.TransactionUtilException
.- Parameters:
ut
- theUserTransaction
instance of the transaction management system in placerunnable
- TheThrowingSupplier
implementation that defines the work.- Throws:
Exception
TransactionUtil.TransactionUtilException
-