Package org.apache.cayenne.tx

# Class BaseTransaction

java.lang.Object
org.apache.cayenne.tx.BaseTransaction

All Implemented Interfaces:
  `Transaction`

Direct Known Subclasses:
  `CayenneTransaction`, `ExternalTransaction`

public abstract class BaseTransaction extends Object implements Transaction
A Cayenne transaction. Currently supports managing JDBC connections.

Since:
  4.0

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected Map<String,Connection>`
  `connections`
  `protected int`
  `defaultIsolationLevel`
  `protected TransactionDescriptor`
  `descriptor`
  `protected Collection<TransactionListener>`
  `listeners`
  `protected int`
  `status`
  `protected static final int`
  `STATUS_ACTIVE`
  `protected static final int`
  `STATUS_COMMITTED`
  `protected static final int`
  `STATUS_COMMITTING`
  `protected static final int`
  `STATUS_MARKED_ROLLEDBACK`
  `protected static final int`
  `STATUS_NO_TRANSACTION`
  `protected static final int`
  `STATUS_ROLLEDBACK`
  `protected static final int`
  `STATUS_ROLLING_BACK`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `BaseTransaction(TransactionDescriptor descriptor)`
  Creates new inactive transaction.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected Connection`
  `addConnection(String connectionName, Connection connection)`
  `void`
  `addListener(TransactionListener listener)`
  `void`
  `begin()`
  Starts a Transaction.
  `static void`
  `bindThreadTransaction(Transaction transaction)`
  Binds a Transaction to the current thread.
  `protected void`
  `close()`
  Closes all connections associated with transaction.
  `void`
  `commit()`
  `protected void`
  `connectionAdded(Connection connection)`
  `Map<String,Connection>`
  `getConnections()`
  Returns all connections associated with the transaction.
  `protected Connection`
  `getExistingConnection(String name)`
  `Connection`
  `getOrCreateConnection(String connectionName, DataSource dataSource)`
  Retrieves a connection for the given symbolic name.
  `static Transaction`
  `getThreadTransaction()`
  Returns a Transaction associated with the current thread, or null if there is no such Transaction.
  `boolean`
  `isRollbackOnly()`
  `protected abstract void`
  `processCommit()`
  `protected abstract void`
  `processRollback()`
  `void`
  `rollback()`
  `void`
  `setRollbackOnly()`

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

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

  ### Methods inherited from interface org.apache.cayenne.tx.Transaction

  `isExternal`

- ## Field Details

  - ### STATUS_ACTIVE

    protected static final int STATUS_ACTIVE
    See Also:
      - Constant Field Values

  - ### STATUS_COMMITTING

    protected static final int STATUS_COMMITTING
    See Also:
      - Constant Field Values

  - ### STATUS_COMMITTED

    protected static final int STATUS_COMMITTED
    See Also:
      - Constant Field Values

  - ### STATUS_ROLLEDBACK

    protected static final int STATUS_ROLLEDBACK
    See Also:
      - Constant Field Values

  - ### STATUS_ROLLING_BACK

    protected static final int STATUS_ROLLING_BACK
    See Also:
      - Constant Field Values

  - ### STATUS_NO_TRANSACTION

    protected static final int STATUS_NO_TRANSACTION
    See Also:
      - Constant Field Values

  - ### STATUS_MARKED_ROLLEDBACK

    protected static final int STATUS_MARKED_ROLLEDBACK
    See Also:
      - Constant Field Values

  - ### connections

    protected Map<String,Connection> connections

  - ### listeners

    protected Collection<TransactionListener> listeners

  - ### status

    protected int status

  - ### defaultIsolationLevel

    protected int defaultIsolationLevel

  - ### descriptor

    protected TransactionDescriptor descriptor

- ## Constructor Details

  - ### BaseTransaction

    protected BaseTransaction(TransactionDescriptor descriptor)
    Creates new inactive transaction.

- ## Method Details

  - ### bindThreadTransaction

    public static void bindThreadTransaction(Transaction transaction)
    Binds a Transaction to the current thread.

  - ### getThreadTransaction

    public static Transaction getThreadTransaction()
    Returns a Transaction associated with the current thread, or null if there is no such Transaction.

  - ### setRollbackOnly

    public void setRollbackOnly()
    Specified by:
      `setRollbackOnly` in interface `Transaction`

  - ### isRollbackOnly

    public boolean isRollbackOnly()
    Specified by:
      `isRollbackOnly` in interface `Transaction`

  - ### addListener

    public void addListener(TransactionListener listener)
    Specified by:
      `addListener` in interface `Transaction`

  - ### begin

    public void begin()
    Starts a Transaction. If Transaction is not started explicitly, it will be started when the first connection is added.
    Specified by:
      `begin` in interface `Transaction`

  - ### commit

    public void commit()
    Specified by:
      `commit` in interface `Transaction`

  - ### processCommit

    protected abstract void processCommit()

  - ### rollback

    public void rollback()
    Specified by:
      `rollback` in interface `Transaction`

  - ### processRollback

    protected abstract void processRollback()

  - ### getConnections

    public Map<String,Connection> getConnections()
    Description copied from interface: `Transaction`
    Returns all connections associated with the transaction.
    Specified by:
      `getConnections` in interface `Transaction`
    Returns:
      connections associated with the transaction.

  - ### getOrCreateConnection

    public Connection getOrCreateConnection(String connectionName, DataSource dataSource) throws SQLException
    Description copied from interface: `Transaction`
    Retrieves a connection for the given symbolic name. If it does not exists, creates a new connection using provided DataSource, and registers it internally.
    Specified by:
      `getOrCreateConnection` in interface `Transaction`
    Parameters:
      `connectionName` - a symbolic name of the connection. Cayenne DataNodes generate a name in the form of "DataNode.Connection.nodename".
      `dataSource` - DataSource that provides new connections.
    Returns:
      a connection that participates in the current transaction.
    Throws:
      `SQLException`

  - ### getExistingConnection

    protected Connection getExistingConnection(String name)

  - ### addConnection

    protected Connection addConnection(String connectionName, Connection connection)

  - ### connectionAdded

    protected void connectionAdded(Connection connection)

  - ### close

    protected void close()
    Closes all connections associated with transaction.