Package org.apache.torque.util

# Class Transaction

java.lang.Object
org.apache.torque.util.Transaction

public final class Transaction extends Object
Encapsulates transaction and connection handling within Torque. If the underlying database does not support transaction or the database pool returns autocommit connections, the commit and rollback methods fallback to simple connection pool handling.

Version:
  $Id: Transaction.java 1839284 2018-08-27 08:57:56Z tv $
Author:
  Stephen Haberman

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static TorqueConnection`
  `begin()`
  Begin a transaction by retrieving a connection from the default database connection pool.
  `static TorqueConnection`
  `begin(String dbName)`
  Begin a transaction by retrieving a connection from the named database connection pool.
  `static void`
  `commit(Connection con)`
  Commit a transaction and close the connection.
  `static TransactionManager`
  `getTransactionManager()`
  Returns the current transaction manager.
  `static void`
  `rollback(Connection con)`
  Roll back a transaction and release the connection.
  `static void`
  `safeRollback(Connection con)`
  Roll back a transaction without throwing errors if they occur.
  `static void`
  `setTransactionManager(TransactionManager transactionManager)`
  Sets the transaction manager to use.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### setTransactionManager

    public static void setTransactionManager(TransactionManager transactionManager)
    Sets the transaction manager to use.
    Parameters:
      `transactionManager` - the transaction manager to use.

  - ### getTransactionManager

    public static TransactionManager getTransactionManager()
    Returns the current transaction manager.
    Returns:
      the current transaction manager.

  - ### begin

    public static TorqueConnection begin() throws TorqueException
    Begin a transaction by retrieving a connection from the default database connection pool. WARNING: If the database does not support transaction or the pool has set autocommit to true on the connection, the database will commit after every statement, regardless of when a commit or rollback is issued.
    Returns:
      The Connection for the transaction.
    Throws:
      `TorqueException` - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

  - ### begin

    public static TorqueConnection begin(String dbName) throws TorqueException
    Begin a transaction by retrieving a connection from the named database connection pool. WARNING: If the database does not support transaction or the pool has set autocommit to true on the connection, the database will commit after every statement, regardless of when a commit or rollback is issued.
    Parameters:
      `dbName` - Name of database.
    Returns:
      The Connection for the transaction.
    Throws:
      `TorqueException` - If the connection cannot be retrieved.

  - ### commit

    public static void commit(Connection con) throws TorqueException
    Commit a transaction and close the connection. If the connection is in autocommit mode or the database does not support transactions, only a connection close is performed
    Parameters:
      `con` - The Connection for the transaction.
    Throws:
      `TorqueException` - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

  - ### rollback

    public static void rollback(Connection con) throws TorqueException
    Roll back a transaction and release the connection. In databases that do not support transactions or if autocommit is true, no rollback will be performed, but the connection will be closed anyway.
    Parameters:
      `con` - The Connection for the transaction.
    Throws:
      `TorqueException` - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

  - ### safeRollback

    public static void safeRollback(Connection con)
    Roll back a transaction without throwing errors if they occur. A null Connection argument is logged at the debug level and other errors are logged at warn level.
    Parameters:
      `con` - The Connection for the transaction.
    See Also:
      - `rollback(Connection)`