- All Implemented Interfaces:
ConnectionProvider,TransactionControl,TransactionManager
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCommitCallback(Runnable callback) Adds aRunnableto be executed if the transaction is committed.voidaddRollbackCallback(Runnable callback) Adds aRunnableto be executed if the transaction is rolled back.voidbegin()Begins a new transaction for the current thread.voidvoidcleanup()Performs cleanup operations for the transaction manager, releasing any resources held.voidcommit()Commits the current transaction.Provides a transaction-bound managed connection to the underlying database.booleanChecks if the current transaction is marked as rollback-only.booleanChecks if there is an active transaction for the current thread.booleanIndicates whether cleanup operations are yet to be performed for the transaction manager (i.e. whether the manager itself "active" and holding onto resources).voidrollback()Rolls back the current transaction.
-
Constructor Details
-
DefaultTransactionManager
-
-
Method Details
-
begin
public void begin()Description copied from interface:TransactionManagerBegins 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:
beginin interfaceTransactionManager
-
begin
- Specified by:
beginin interfaceTransactionManager
-
connection
Description copied from interface:ConnectionProviderProvides 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
Connectionlifecycle methods) are managed by aTransactionManagerand are not supported by the returnedManagedConnection.- Specified by:
connectionin interfaceConnectionProvider- Returns:
- a
ManagedConnectioninstance representing the database connection - Throws:
SQLException- if an error occurs while obtaining the connection
-
commit
public void commit()Description copied from interface:TransactionControlCommits 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:
commitin interfaceTransactionControl
-
rollback
public void rollback()Description copied from interface:TransactionControlRolls 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:
rollbackin interfaceTransactionControl
-
isTransactionActive
public boolean isTransactionActive()Description copied from interface:TransactionControlChecks 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:
isTransactionActivein interfaceTransactionControl- Returns:
trueif a thread-bound transaction is currently active,falseotherwise
-
isRollbackOnly
public boolean isRollbackOnly()Description copied from interface:TransactionControlChecks 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:
isRollbackOnlyin interfaceTransactionControl- Returns:
trueif the transaction is marked for rollback,falseotherwise
-
cleanup
Description copied from interface:TransactionManagerPerforms 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()andTransactionControl.rollback().- Specified by:
cleanupin interfaceTransactionManager- Throws:
TransactionException- if an error occurs during cleanup operations
-
requiresCleanup
public boolean requiresCleanup()Description copied from interface:TransactionManagerIndicates 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
trueif cleanup still needs to be performed,falseotherwise.- Specified by:
requiresCleanupin interfaceTransactionManager- Returns:
trueif cleanup still needs to be performed,falseotherwise
-
addCommitCallback
Description copied from interface:TransactionManagerAdds aRunnableto be executed if the transaction is committed.- Specified by:
addCommitCallbackin interfaceTransactionManager- Parameters:
callback- The runnable to execute when the transaction is committed.
-
addRollbackCallback
Description copied from interface:TransactionManagerAdds aRunnableto be executed if the transaction is rolled back.- Specified by:
addRollbackCallbackin interfaceTransactionManager- Parameters:
callback- The runnable to execute on transaction rollback.
-