Package com.ultikits.ultitools.manager
Class DataSourceTransactionManager
java.lang.Object
com.ultikits.ultitools.manager.DataSourceTransactionManager
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classInternal class to hold transaction state. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal<DataSourceTransactionManager.TransactionContext> ThreadLocal holding the current transaction context for each thread.private final DataSourceprivate static final Logger -
Constructor Summary
ConstructorsConstructorDescriptionDataSourceTransactionManager(DataSource dataSource) Creates a new DataSourceTransactionManager. -
Method Summary
Modifier and TypeMethodDescriptionvoidbegin()Begins a new transaction.private voidCleans up the transaction context.voidcommit()Commits the current transaction.Gets the current connection, creating a new one if necessary.(package private) static DataSourceTransactionManager.TransactionContextGets the current transaction context (for testing).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.
-
Field Details
-
LOGGER
-
dataSource
-
contextHolder
ThreadLocal holding the current transaction context for each thread.
-
-
Constructor Details
-
DataSourceTransactionManager
Creates a new DataSourceTransactionManager.- Parameters:
dataSource- the data source to use for connections
-
-
Method Details
-
getConnection
Description copied from interface:TransactionManagerGets 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:
getConnectionin interfaceTransactionManager- Returns:
- the current connection
-
begin
public void begin()Description copied from interface:TransactionManagerBegins a new transaction.Creates a new connection, disables auto-commit, and associates it with the current thread.
- Specified by:
beginin interfaceTransactionManager
-
commit
public void commit()Description copied from interface:TransactionManagerCommits the current transaction.Commits all changes made during the transaction and releases the connection.
- Specified by:
commitin interfaceTransactionManager
-
rollback
public void rollback()Description copied from interface:TransactionManagerRolls back the current transaction.Discards all changes made during the transaction and releases the connection.
- Specified by:
rollbackin interfaceTransactionManager
-
hasActiveTransaction
public boolean hasActiveTransaction()Description copied from interface:TransactionManagerChecks if there is an active transaction on the current thread.- Specified by:
hasActiveTransactionin interfaceTransactionManager- Returns:
- true if a transaction is active
-
setIsolationLevel
public void setIsolationLevel(int level) Description copied from interface:TransactionManagerSets the isolation level for the current transaction.Must be called after begin() and before any statements are executed.
- Specified by:
setIsolationLevelin interfaceTransactionManager- Parameters:
level- the JDBC isolation level constant
-
setReadOnly
public void setReadOnly(boolean readOnly) Description copied from interface:TransactionManagerSets read-only mode for the current transaction.- Specified by:
setReadOnlyin interfaceTransactionManager- Parameters:
readOnly- true for read-only mode
-
setTimeout
public void setTimeout(int seconds) Description copied from interface:TransactionManagerSets the timeout for the current transaction.Note: Not all databases/drivers support transaction timeout.
- Specified by:
setTimeoutin interfaceTransactionManager- Parameters:
seconds- the timeout in seconds
-
getTransactionDepth
public int getTransactionDepth()Description copied from interface:TransactionManagerGets the current transaction depth (for nested transactions).- Specified by:
getTransactionDepthin interfaceTransactionManager- Returns:
- the transaction depth (0 if no transaction)
-
cleanup
Cleans up the transaction context. -
getCurrentContext
Gets the current transaction context (for testing).
-