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>

public class Transaction<T> extends Object implements 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 Details

    • Transaction

      protected Transaction(Sql<T> theSql)
      Constructs a transaction stage with the given SQL operation.
      Parameters:
      theSql - the SQL operation to be executed
  • Method Details

    • begin

      public static <T> Transaction<T> begin(Sql<T> sql)
      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 Transaction instance
    • thenApply

      public <R> Transaction<R> thenApply(Function<T,Sql<R>> tSqlFunction)
      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 Transaction instance
    • execute

      public T execute(Connection connection) throws SQLException
      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:
      execute in interface Sql<T>
      Parameters:
      connection - the database connection
      Returns:
      the result of the SQL execution
      Throws:
      SQLException - if a database error occurs
    • savePoint

      public <R> Transaction<R> savePoint(String savePointId, Function<T, Transaction<R>> transactionFn)
      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 savepoint
      transactionFn - function mapping the result of this Transaction to next Transaction
      Returns:
      the current Transaction instance for fluent chaining