- All Implemented Interfaces:
AutoCloseable,ConnectionProvider,TransactionControl
A transaction is used to perform operations such as commit and rollback on a transactional resource. It also provides methods for obtaining connections and checking the state of the transaction.
Instances of this class are created with a TransactionManager,
which provides the underlying transaction management capabilities.
The lifecycle of a transaction includes starting, committing, rolling
back, or closing. A transaction can be completed either by calling
commit(), rollback(), or automatically when the
close() method is invoked.
This class also implements AutoCloseable, ensuring that resources are
properly released if the transaction block terminates without explicit commit.
If a transaction is not committed when closed, it will be rolled back.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()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.voidrollback()Rolls back the current transaction.
-
Constructor Details
-
Transaction
-
-
Method Details
-
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
-
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
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-