Class Transaction

java.lang.Object
ortus.boxlang.runtime.jdbc.Transaction
All Implemented Interfaces:
ITransaction

public class Transaction extends Object implements ITransaction
A transaction object that wraps a JDBC connection and provides transactional operations.
  • Constructor Details

    • Transaction

      public Transaction(IBoxContext context, DataSource datasource)
      Constructor.

      Note this constructor does NOT accept or open a connecton upon construction. This is so we avoid acquiring connections we don't use; i.e. for a transaction that may never execute a query. Instead, we only open the connection when getConnection() is first called.

      Parameters:
      datasource - The datasource associated with this transaction
  • Method Details

    • setIsolationLevel

      public Transaction setIsolationLevel(int isolationLevel)
      Set isolation level.
      Specified by:
      setIsolationLevel in interface ITransaction
      Returns:
      The transaction object for chainability.
    • getIsolationLevel

      public int getIsolationLevel()
      Get the configured transaction isolation level.
      Specified by:
      getIsolationLevel in interface ITransaction
    • getConnection

      public Connection getConnection()
      Get (creating if none found) the connection associated with this transaction.

      This method should be called by queries executed inside a transaction body to ensure they run on the correct (transactional) connection. Upon first execution, this method will acquire a connection from the datasource and store it for further use within the transaction.

      Specified by:
      getConnection in interface ITransaction
    • setDataSource

      public Transaction setDataSource(DataSource datasource)
      Set the datasource associated with this transaction.

      For transactions not initialized with a datasource, allows you to set the datasource after construction.

      Will throw an exception if the datasource is already set.

      Specified by:
      setDataSource in interface ITransaction
    • getDataSource

      public DataSource getDataSource()
      Get the datasource associated with this transaction.

      Useful for checking that a given query is using the same datasource as its wrapping transaction.

      Specified by:
      getDataSource in interface ITransaction
    • begin

      public Transaction begin()
      Begin the transaction - essentially a no-nop, as the transaction is only started when the connection is first acquired.
      Specified by:
      begin in interface ITransaction
    • commit

      public Transaction commit()
      Commit the transaction
      Specified by:
      commit in interface ITransaction
      Returns:
      The transaction object for chainability.
    • rollback

      public Transaction rollback()
      Rollback the entire transaction. The transaction will be rolled back to the last committed point, and will ignore any set savepoints.
      Specified by:
      rollback in interface ITransaction
      Returns:
      The transaction object for chainability.
    • rollback

      public Transaction rollback(Key savepoint)
      Rollback the transaction up to the last (named) savepoint.
      Specified by:
      rollback in interface ITransaction
      Parameters:
      savepoint - The name of the savepoint to rollback to or NULL for no savepoint.
      Returns:
      The transaction object for chainability.
    • setSavepoint

      public Savepoint setSavepoint(Key savepoint)
      Set a savepoint in the transaction
      Specified by:
      setSavepoint in interface ITransaction
      Parameters:
      savepoint - The name of the savepoint
      Returns:
      The created JDBC savepoint object or NULL if the savepoint could not be created.
    • end

      public Transaction end()
      Shutdown the transaction by re-enabling auto commit mode and closing the connection to the database (i.e. releasing it back to the connection pool from whence it came.)
      Specified by:
      end in interface ITransaction
      Returns:
      The transaction object for chainability.
    • getSavepoints

      public Map<Key,Savepoint> getSavepoints()
      Get the savepoints used in this transaction.

      This method returns a map of savepoint names to their associated savepoint objects, allowing you to manage and rollback to specific points in the transaction.

      Specified by:
      getSavepoints in interface ITransaction
      Returns:
      A map of savepoint Keys to JDBC savepoint objects.
      See Also: