Class TransactionTemplate

java.lang.Object
ru.vyarus.guicey.jdbi3.tx.TransactionTemplate

@Singleton public class TransactionTemplate extends Object
Transaction template used to both declare unit of work and start transaction. If called inside of transaction then provided action will be simply executed as transaction is already managed somewhere outside. In case of exception, it's propagated and transaction rolled back.

Usage:


    @Inject TransactionTemplate template;
     ...
     template.inTransaction(() -> doSoemStaff())
 
Since:
31.08.2018
  • Constructor Details

    • TransactionTemplate

      @Inject public TransactionTemplate(UnitManager manager)
      Create transactional template.
      Parameters:
      manager - unit manager
  • Method Details

    • inTransaction

      public <T> T inTransaction(TxAction<T> action)
      Shortcut for inTransaction(TxConfig, TxAction) for calling action with default transaction config.
      Type Parameters:
      T - return type
      Parameters:
      action - action to execute
      Returns:
      action result
    • inTransaction

      public <T> T inTransaction(TxConfig config, TxAction<T> action)
      Wraps provided action with unit of work and transaction. If called under already started transaction then action will be called directly.

      NOTE: If unit of work was started manually (using UnitManager, but without transaction started, then action will be simply executed without starting transaction. This was done for rare situations when logic must be performed without transaction and transaction annotation will simply indicate unit of work.

      Type Parameters:
      T - return type
      Parameters:
      config - transaction config
      action - action to execute
      Returns:
      action result