Class AbstractTransactionOperations<T extends InternalTransaction<C>,C>

java.lang.Object
io.micronaut.transaction.support.AbstractPropagatedStatusTransactionOperations<T,C>
io.micronaut.transaction.support.AbstractTransactionOperations<T,C>
Type Parameters:
T - The transaction type
C - The connection type
All Implemented Interfaces:
SynchronousTransactionManager<C>, TransactionManager, TransactionOperations<C>
Direct Known Subclasses:
AbstractDefaultTransactionOperations

@Internal public abstract class AbstractTransactionOperations<T extends InternalTransaction<C>,C> extends AbstractPropagatedStatusTransactionOperations<T,C> implements TransactionOperations<C>, SynchronousTransactionManager<C>
Abstract transaction operations. Partially based on https://github.com/spring-projects/spring-framework/blob/main/spring-tx/src/main/java/org/springframework/transaction/support/AbstractPlatformTransactionManager.java
Since:
4.0.0
Author:
Denis Stepanov
  • Field Details

    • logger

      protected final org.slf4j.Logger logger
    • connectionOperations

      protected final ConnectionOperations<C> connectionOperations
    • synchronousConnectionManager

      @Nullable protected final @Nullable SynchronousConnectionManager<C> synchronousConnectionManager
  • Constructor Details

  • Method Details

    • getConnectionDefinition

      protected ConnectionDefinition getConnectionDefinition(TransactionDefinition transactionDefinition)
      Returns connection definition.
      Parameters:
      transactionDefinition - The transaction definition
      Returns:
      connection definition
    • createNoTxTransactionStatus

      protected abstract T createNoTxTransactionStatus(@NonNull @NonNull ConnectionStatus<C> connectionStatus, @NonNull @NonNull TransactionDefinition definition)
      Create transaction status without open transaction.
      Parameters:
      connectionStatus - The connection status
      definition - The transaction definition
      Returns:
      new transaction status
    • createNewTransactionStatus

      protected abstract T createNewTransactionStatus(@NonNull @NonNull ConnectionStatus<C> connectionStatus, @NonNull @NonNull TransactionDefinition definition)
      Create transaction status with new transaction.
      Parameters:
      connectionStatus - The connection status
      definition - The transaction definition
      Returns:
      new transaction status
    • createExistingTransactionStatus

      protected abstract T createExistingTransactionStatus(@NonNull @NonNull ConnectionStatus<C> connectionStatus, @NonNull @NonNull TransactionDefinition definition, @NonNull T existingTransaction)
      Create transaction status with existing transaction.
      Parameters:
      connectionStatus - The connection status
      definition - The transaction definition
      existingTransaction - The existing transaction
      Returns:
      new transaction status
    • hasConnection

      public boolean hasConnection()
      Description copied from interface: TransactionOperations
      Check if the connection exists.
      Specified by:
      hasConnection in interface TransactionOperations<T extends InternalTransaction<C>>
      Returns:
      True if transaction exists
    • doExecute

      protected final <R> R doExecute(@NonNull @NonNull TransactionDefinition definition, @NonNull @NonNull TransactionCallback<C,R> callback)
      Description copied from class: AbstractPropagatedStatusTransactionOperations
      Do execute in transaction.
      Specified by:
      doExecute in class AbstractPropagatedStatusTransactionOperations<T extends InternalTransaction<C>,C>
      Type Parameters:
      R - The result type
      Parameters:
      definition - The transaction definition
      callback - The callback
      Returns:
      The result
    • doBegin

      protected abstract void doBegin(T tx)
      Begin a new transaction with semantics according to the given transaction definition. Does not have to care about applying the propagation behavior, as this has already been handled by this abstract manager.

      This method gets called when the transaction manager has decided to actually start a new transaction. Either there wasn't any transaction before, or the previous transaction has been suspended.

      Parameters:
      tx - The transaction
    • doCommit

      protected abstract void doCommit(T tx)
      Perform an actual commit of the given transaction.

      An implementation does not need to check the "new transaction" flag or the rollback-only flag; this will already have been handled before. Usually, a straight commit will be performed on the transaction object contained in the passed-in status.

      Parameters:
      tx - The transaction
    • doRollback

      protected abstract void doRollback(T tx)
      Perform an actual rollback of the given transaction.

      An implementation does not need to check the "new transaction" flag; this will already have been handled before. Usually, a straight rollback will be performed on the transaction object contained in the passed-in status.

      Parameters:
      tx - The transaction
    • doNestedBegin

      protected void doNestedBegin(T tx)
      Alternative of the doBegin(InternalTransaction) intended for a nested transaction.
      Parameters:
      tx - The transaction
    • doNestedCommit

      protected void doNestedCommit(T tx)
      Alternative of the doCommit(InternalTransaction) intended for a nested transaction.
      Parameters:
      tx - The transaction
    • doNestedRollback

      protected void doNestedRollback(T tx)
      Alternative of the doRollback(InternalTransaction) intended for a nested transaction.
      Parameters:
      tx - The transaction
    • determineTimeout

      protected Optional<Duration> determineTimeout(TransactionDefinition definition)
      Determine the actual timeout to use for the given definition. Will fall back to this manager's default timeout if the transaction definition doesn't specify a non-default value.
      Parameters:
      definition - the transaction definition
      Returns:
      the optional timeout to use
      See Also:
    • doSuspend

      protected void doSuspend(T transaction)
      Do suspend the transaction.
      Parameters:
      transaction - The transaction
    • doResume

      protected void doResume(T transaction)
      Do resume the transaction.
      Parameters:
      transaction - The transaction
    • suspend

      protected <R> R suspend(T transaction, Supplier<R> callback)
      Do suspend the transaction.
      Type Parameters:
      R - The result type
      Parameters:
      transaction - The transaction
      callback - The callback
      Returns:
      The callback result
    • getTransaction

      @NonNull public @NonNull TransactionStatus<C> getTransaction(TransactionDefinition definition) throws TransactionException
      Description copied from interface: SynchronousTransactionManager
      Return a currently active transaction or create a new one, according to the specified propagation behavior.

      Note that parameters like isolation level or timeout will only be applied to new transactions, and thus be ignored when participating in active ones.

      Furthermore, not all transaction definition settings will be supported by every transaction manager: A proper transaction manager implementation should throw an exception when unsupported settings are encountered.

      An exception to the above rule is the read-only flag, which should be ignored if no explicit read-only mode is supported. Essentially, the read-only flag is just a hint for potential optimization.

      Specified by:
      getTransaction in interface SynchronousTransactionManager<T extends InternalTransaction<C>>
      Parameters:
      definition - the TransactionDefinition instance (can be null for defaults), describing propagation behavior, isolation level, timeout etc.
      Returns:
      transaction status object representing the new or current transaction
      Throws:
      TransactionException - in case of lookup, creation, or system errors
      IllegalTransactionStateException - if the given transaction definition cannot be executed (for example, if a currently active transaction is in conflict with the specified propagation behavior)
      See Also:
    • commit

      public void commit(TransactionStatus<C> status) throws TransactionException
      Description copied from interface: SynchronousTransactionManager
      Commit the given transaction, with regard to its status. If the transaction has been marked rollback-only programmatically, perform a rollback.

      If the transaction wasn't a new one, omit the commit for proper participation in the surrounding transaction. If a previous transaction has been suspended to be able to create a new one, resume the previous transaction after committing the new one.

      Note that when the commit call completes, no matter if normally or throwing an exception, the transaction must be fully completed and cleaned up. No rollback call should be expected in such a case.

      If this method throws an exception other than a TransactionException, then some before-commit error caused the commit attempt to fail. For example, an O/R Mapping tool might have tried to flush changes to the database right before commit, with the resulting DataAccessException causing the transaction to fail. The original exception will be propagated to the caller of this commit method in such a case.

      Specified by:
      commit in interface SynchronousTransactionManager<T extends InternalTransaction<C>>
      Parameters:
      status - object returned by the getTransaction method
      Throws:
      UnexpectedRollbackException - in case of an unexpected rollback that the transaction coordinator initiated
      HeuristicCompletionException - in case of a transaction failure caused by a heuristic decision on the side of the transaction coordinator
      TransactionSystemException - in case of commit or system errors (typically caused by fundamental resource failures)
      IllegalTransactionStateException - if the given transaction is already completed (that is, committed or rolled back)
      TransactionException - if something goes wrong during commit
      See Also:
    • rollback

      public void rollback(TransactionStatus<C> status) throws TransactionException
      Description copied from interface: SynchronousTransactionManager
      Perform a rollback of the given transaction.

      If the transaction wasn't a new one, just set it rollback-only for proper participation in the surrounding transaction. If a previous transaction has been suspended to be able to create a new one, resume the previous transaction after rolling back the new one.

      Do not call rollback on a transaction if commit threw an exception. The transaction will already have been completed and cleaned up when commit returns, even in case of a commit exception. Consequently, a rollback call after commit failure will lead to an IllegalTransactionStateException.

      Specified by:
      rollback in interface SynchronousTransactionManager<T extends InternalTransaction<C>>
      Parameters:
      status - object returned by the getTransaction method
      Throws:
      TransactionSystemException - in case of rollback or system errors (typically caused by fundamental resource failures)
      IllegalTransactionStateException - if the given transaction is already completed (that is, committed or rolled back)
      TransactionException - if something goes wrong during rollback