Class Transaction

java.lang.Object
org.litebridgedb.orm.tx.Transaction
All Implemented Interfaces:
AutoCloseable, ConnectionProvider, TransactionControl

public final class Transaction extends Object implements TransactionControl, AutoCloseable
Represents a transactional context that allows controlled execution of operations within a transaction. This class is immutable and thread-safe.

A transaction is used to perform operations such as commit and rollback on a transactional resource. It also provides methods for obtaining connections and checking the state of the transaction.

Instances of this class are created with a TransactionManager, which provides the underlying transaction management capabilities. The lifecycle of a transaction includes starting, committing, rolling back, or closing. A transaction can be completed either by calling commit(), rollback(), or automatically when the close() method is invoked.

This class also implements AutoCloseable, ensuring that resources are properly released if the transaction block terminates without explicit commit. If a transaction is not committed when closed, it will be rolled back.

  • Constructor Details

  • Method Details

    • commit

      public void commit()
      Description copied from interface: TransactionControl
      Commits the current transaction.

      This method finalises all operations performed during the transaction and applies any changes to the underlying database. After a successful commit, the transaction is no longer active.

      Specified by:
      commit in interface TransactionControl
    • rollback

      public void rollback()
      Description copied from interface: TransactionControl
      Rolls back the current transaction.

      This method undoes all changes made during the active transaction. It reverts the state of the underlying data store to the point before the transaction began. After a successful rollback, the transaction is no longer active.

      It should be called in cases where the transaction cannot be successfully completed, such as when an error occurs during the operations performed within the transaction.

      Specified by:
      rollback in interface TransactionControl
    • isTransactionActive

      public boolean isTransactionActive()
      Description copied from interface: TransactionControl
      Checks if there is an active transaction for the current thread.

      This method determines whether a transaction has been started and is not yet committed or rolled back.

      Specified by:
      isTransactionActive in interface TransactionControl
      Returns:
      true if a thread-bound transaction is currently active, false otherwise
    • isRollbackOnly

      public boolean isRollbackOnly()
      Description copied from interface: TransactionControl
      Checks if the current transaction is marked as rollback-only. This indicates that the transaction cannot be successfully committed and must be rolled back.
      Specified by:
      isRollbackOnly in interface TransactionControl
      Returns:
      true if the transaction is marked for rollback, false otherwise
    • connection

      public ManagedConnection connection() throws SQLException
      Description copied from interface: ConnectionProvider
      Provides a transaction-bound managed connection to the underlying database.

      This method is typically used to execute database operations within the context of the current transaction (or not, if the connection is in auto-commit mode).

      Transactions themselves (and corresponding Connection lifecycle methods) are managed by a TransactionManager and are not supported by the returned ManagedConnection.

      Specified by:
      connection in interface ConnectionProvider
      Returns:
      a ManagedConnection instance representing the database connection
      Throws:
      SQLException - if an error occurs while obtaining the connection
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable