Class DataSourceTransactionManager

java.lang.Object
com.ultikits.ultitools.manager.DataSourceTransactionManager
All Implemented Interfaces:
TransactionManager

@Internal public class DataSourceTransactionManager extends Object implements TransactionManager
DataSource-based implementation of TransactionManager.

Uses ThreadLocal to associate transactions with threads, ensuring thread safety. Each thread can have at most one active transaction at a time.

Since:
6.2.0
Author:
wisdomme
  • Field Details

  • Constructor Details

    • DataSourceTransactionManager

      public DataSourceTransactionManager(DataSource dataSource)
      Creates a new DataSourceTransactionManager.
      Parameters:
      dataSource - the data source to use for connections
  • Method Details

    • getConnection

      public Connection getConnection()
      Description copied from interface: TransactionManager
      Gets the current connection, creating a new one if necessary.

      If a transaction is active, returns the connection associated with it. Otherwise, returns a new connection from the data source.

      Specified by:
      getConnection in interface TransactionManager
      Returns:
      the current connection
    • begin

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

      Creates a new connection, disables auto-commit, and associates it with the current thread.

      Specified by:
      begin in interface TransactionManager
    • commit

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

      Commits all changes made during the transaction and releases the connection.

      Specified by:
      commit in interface TransactionManager
    • rollback

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

      Discards all changes made during the transaction and releases the connection.

      Specified by:
      rollback in interface TransactionManager
    • hasActiveTransaction

      public boolean hasActiveTransaction()
      Description copied from interface: TransactionManager
      Checks if there is an active transaction on the current thread.
      Specified by:
      hasActiveTransaction in interface TransactionManager
      Returns:
      true if a transaction is active
    • setIsolationLevel

      public void setIsolationLevel(int level)
      Description copied from interface: TransactionManager
      Sets the isolation level for the current transaction.

      Must be called after begin() and before any statements are executed.

      Specified by:
      setIsolationLevel in interface TransactionManager
      Parameters:
      level - the JDBC isolation level constant
    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Description copied from interface: TransactionManager
      Sets read-only mode for the current transaction.
      Specified by:
      setReadOnly in interface TransactionManager
      Parameters:
      readOnly - true for read-only mode
    • setTimeout

      public void setTimeout(int seconds)
      Description copied from interface: TransactionManager
      Sets the timeout for the current transaction.

      Note: Not all databases/drivers support transaction timeout.

      Specified by:
      setTimeout in interface TransactionManager
      Parameters:
      seconds - the timeout in seconds
    • getTransactionDepth

      public int getTransactionDepth()
      Description copied from interface: TransactionManager
      Gets the current transaction depth (for nested transactions).
      Specified by:
      getTransactionDepth in interface TransactionManager
      Returns:
      the transaction depth (0 if no transaction)
    • cleanup

      Cleans up the transaction context.
    • getCurrentContext

      Gets the current transaction context (for testing).