Class Transaction<T>
java.lang.Object
org.tamilnadujug.Transaction<T>
- Type Parameters:
T- the result type of the SQL operation at the current stage
- All Implemented Interfaces:
Sql<T>
Represents a database transaction as a composable unit of work.
Implements the
Sql interface, allowing execution of SQL
operations within a transactional context. You can chain multiple SQL
operations together, passing the output of one as input to the next.
Usage Example:
Transaction tx = Transaction.begin(insertUserSql)
.thenApply(userId -> updateAccountSql(userId))
.thenApply(accountId -> fetchDetailsSql(accountId));
tx.execute(dataSource);
Transaction Management:
- Disables auto-commit before execution
- Executes the SQL chain atomically
- Commits after successful execution
- Caller handles rollback on exceptions
- Restores auto-commit at the end
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTransaction(Sql<T> theSql) Constructs a transaction stage with the given SQL operation. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Transaction<T> Begins a new transaction with the initial SQL operation.execute(Connection connection) Executes the transaction on the given database connection.<R> Transaction<R> savePoint(String savePointId, Function<T, Transaction<R>> transactionFn) Creates a savepoint within the current transaction.<R> Transaction<R> Chains another SQL operation to be executed after this transaction stage.
-
Constructor Details
-
Transaction
-
-
Method Details
-
begin
Begins a new transaction with the initial SQL operation.- Type Parameters:
T- the result type of the SQL operation- Parameters:
sql- the first SQL operation- Returns:
- a new
Transactioninstance
-
thenApply
Chains another SQL operation to be executed after this transaction stage.The result of the current SQL operation is passed into the provided function, which returns the next SQL operation to execute.
- Type Parameters:
R- the result type of the next SQL operation- Parameters:
tSqlFunction- function mapping the result of this SQL to next SQL- Returns:
- a new
Transactioninstance
-
execute
Executes the transaction on the given database connection.Manages transaction boundaries automatically:
- Disables auto-commit before execution
- Commits the transaction upon success
- Restores auto-commit afterward
- Specified by:
executein interfaceSql<T>- Parameters:
connection- the database connection- Returns:
- the result of the SQL execution
- Throws:
SQLException- if a database error occurs
-
savePoint
Creates a savepoint within the current transaction.Savepoints allow you to mark a specific point in a transaction that you can roll back to later without affecting the entire transaction. This is useful when you want to group a set of operations together, but still have the option to undo them if needed, while keeping earlier successful operations intact.
Transaction .begin(...) .savePoint("sp1") .thenApply(...) .rollBackTo("sp1") .execute(dataSource);- Type Parameters:
R- the result type of the next SQL operation- Parameters:
savePointId- a unique identifier for the savepointtransactionFn- function mapping the result of this Transaction to next Transaction- Returns:
- the current
Transactioninstance for fluent chaining
-