Interface TransactionManager
- All Known Implementing Classes:
DataSourceTransactionManager
public interface TransactionManager
Interface for managing database transactions.
Implementations handle transaction boundaries (begin, commit, rollback) and provide connections that are aware of the current transaction context.
- Since:
- 6.2.0
- Author:
- wisdomme
-
Method Summary
Modifier and TypeMethodDescriptionvoidbegin()Begins a new transaction.voidcommit()Commits the current transaction.Gets the current connection, creating a new one if necessary.intGets the current transaction depth (for nested transactions).booleanChecks if there is an active transaction on the current thread.voidrollback()Rolls back the current transaction.voidsetIsolationLevel(int level) Sets the isolation level for the current transaction.voidsetReadOnly(boolean readOnly) Sets read-only mode for the current transaction.voidsetTimeout(int seconds) Sets the timeout for the current transaction.
-
Method Details
-
getConnection
Connection getConnection()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.
- Returns:
- the current connection
-
begin
void begin()Begins a new transaction.Creates a new connection, disables auto-commit, and associates it with the current thread.
- Throws:
DataAccessException- if transaction cannot be started
-
commit
void commit()Commits the current transaction.Commits all changes made during the transaction and releases the connection.
- Throws:
DataAccessException- if commit fails
-
rollback
void rollback()Rolls back the current transaction.Discards all changes made during the transaction and releases the connection.
-
hasActiveTransaction
boolean hasActiveTransaction()Checks if there is an active transaction on the current thread.- Returns:
- true if a transaction is active
-
setIsolationLevel
void setIsolationLevel(int level) Sets the isolation level for the current transaction.Must be called after begin() and before any statements are executed.
- Parameters:
level- the JDBC isolation level constant
-
setReadOnly
void setReadOnly(boolean readOnly) Sets read-only mode for the current transaction.- Parameters:
readOnly- true for read-only mode
-
setTimeout
void setTimeout(int seconds) Sets the timeout for the current transaction.Note: Not all databases/drivers support transaction timeout.
- Parameters:
seconds- the timeout in seconds
-
getTransactionDepth
int getTransactionDepth()Gets the current transaction depth (for nested transactions).- Returns:
- the transaction depth (0 if no transaction)
-