Class TransactionUtil

java.lang.Object
com.zfabrik.tx.TransactionUtil

public class TransactionUtil extends Object
This class provides utilities around transaction management with Z2's built-in JTA implementation.
  • Constructor Details

    • TransactionUtil

      public TransactionUtil()
  • Method Details

    • run

      public static <V> V run(javax.transaction.UserTransaction ut, Callable<V> callable) throws Exception
      Use 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.

      Parameters:
      ut - the UserTransaction instance of the transaction management system in place
      callable - The Callable implementation that defines the work.
      Returns:
      The result of the callable invocation.
      Throws:
      Exception
    • get

      public static <V,​ E extends Exception> V get(javax.transaction.UserTransaction ut, ThrowingSupplier<V,​E> supplier) throws E, TransactionUtil.TransactionUtilException
      Use 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 a TransactionUtil.TransactionUtilException.

      Parameters:
      ut - the UserTransaction instance of the transaction management system in place
      supplier - The ThrowingSupplier 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.TransactionUtilException
      Use 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 a TransactionUtil.TransactionUtilException.

      Parameters:
      ut - the UserTransaction instance of the transaction management system in place
      runnable - The ThrowingSupplier implementation that defines the work.
      Throws:
      Exception
      TransactionUtil.TransactionUtilException