Class DefaultTransactionManager

java.lang.Object
org.litebridgedb.orm.tx.DefaultTransactionManager
All Implemented Interfaces:
ConnectionProvider, TransactionControl, TransactionManager

public final class DefaultTransactionManager extends Object implements TransactionManager
Default Litebridge Transaction Manager

The DefaultTransactionManager provides transaction management using a ThreadLocal to maintain a transaction-bound Connection for each thread. It ensures that all database operations within a transaction are performed using the same connection and that cleanup is handled properly.

Key features: - Manages transactions per thread using a ThreadLocal. - Initiates, commits, or rolls back transactions. - Provides a transaction-bound connection for database interaction. - Performs automatic cleanup of resources when a transaction ends.

This class is thread-safe in the sense that each thread is provided with its own transaction state, but it is not suitable for use in scenarios where multiple threads share the same transaction.

Common Exceptions: - IllegalStateException is thrown when a method requiring a transaction is invoked without an active transaction - TransactionException is thrown for failures during transaction begin, commit, or rollback operations. - SQLException is propagated when querying for a new connection or performing cleanup operations.

  • Constructor Details

    • DefaultTransactionManager

      public DefaultTransactionManager(DataSource dataSource)
  • Method Details

    • begin

      public void begin()
      Description copied from interface: TransactionManager
      Begins a new transaction for the current thread.

      This method should be called before performing operations that need to be executed within a transactional context. It ensures that subsequent database interactions are part of the same transaction until it is either committed or rolled back.

      This method may be called multiple times (nested).

      Specified by:
      begin in interface TransactionManager
    • begin

      public void begin(boolean readOnly, Isolation isolation)
      Specified by:
      begin in interface TransactionManager
    • connection

      public ManagedConnection connection() throws SQLException
      Description copied from interface: ConnectionProvider
      Provides a transaction-bound managed connection to the underlying database.

      This method is typically used to execute database operations within the context of the current transaction (or not, if the connection is in auto-commit mode).

      Transactions themselves (and corresponding Connection lifecycle methods) are managed by a TransactionManager and are not supported by the returned ManagedConnection.

      Specified by:
      connection in interface ConnectionProvider
      Returns:
      a ManagedConnection instance representing the database connection
      Throws:
      SQLException - if an error occurs while obtaining the connection
    • commit

      public void commit()
      Description copied from interface: TransactionControl
      Commits the current transaction.

      This method finalises all operations performed during the transaction and applies any changes to the underlying database. After a successful commit, the transaction is no longer active.

      Specified by:
      commit in interface TransactionControl
    • rollback

      public void rollback()
      Description copied from interface: TransactionControl
      Rolls back the current transaction.

      This method undoes all changes made during the active transaction. It reverts the state of the underlying data store to the point before the transaction began. After a successful rollback, the transaction is no longer active.

      It should be called in cases where the transaction cannot be successfully completed, such as when an error occurs during the operations performed within the transaction.

      Specified by:
      rollback in interface TransactionControl
    • isTransactionActive

      public boolean isTransactionActive()
      Description copied from interface: TransactionControl
      Checks if there is an active transaction for the current thread.

      This method determines whether a transaction has been started and is not yet committed or rolled back.

      Specified by:
      isTransactionActive in interface TransactionControl
      Returns:
      true if a thread-bound transaction is currently active, false otherwise
    • isRollbackOnly

      public boolean isRollbackOnly()
      Description copied from interface: TransactionControl
      Checks if the current transaction is marked as rollback-only. This indicates that the transaction cannot be successfully committed and must be rolled back.
      Specified by:
      isRollbackOnly in interface TransactionControl
      Returns:
      true if the transaction is marked for rollback, false otherwise
    • cleanup

      public void cleanup() throws TransactionException
      Description copied from interface: TransactionManager
      Performs cleanup operations for the transaction manager, releasing any resources held. This method should be called when the transaction manager is no longer needed to ensure proper resource management.

      This method is idempotent; subsequent calls do nothing after the first cleanup.

      This is typically called directly by implementations of TransactionControl.commit() and TransactionControl.rollback().

      Specified by:
      cleanup in interface TransactionManager
      Throws:
      TransactionException - if an error occurs during cleanup operations
    • requiresCleanup

      public boolean requiresCleanup()
      Description copied from interface: TransactionManager
      Indicates whether cleanup operations are yet to be performed for the transaction manager (i.e. whether the manager itself "active" and holding onto resources).

      This method returns true if cleanup still needs to be performed, false otherwise.

      Specified by:
      requiresCleanup in interface TransactionManager
      Returns:
      true if cleanup still needs to be performed, false otherwise
    • addCommitCallback

      public void addCommitCallback(Runnable callback)
      Description copied from interface: TransactionManager
      Adds a Runnable to be executed if the transaction is committed.
      Specified by:
      addCommitCallback in interface TransactionManager
      Parameters:
      callback - The runnable to execute when the transaction is committed.
    • addRollbackCallback

      public void addRollbackCallback(Runnable callback)
      Description copied from interface: TransactionManager
      Adds a Runnable to be executed if the transaction is rolled back.
      Specified by:
      addRollbackCallback in interface TransactionManager
      Parameters:
      callback - The runnable to execute on transaction rollback.