# Class ReadOnlyTransaction

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

All Implemented Interfaces:
  `Transaction`

public class ReadOnlyTransaction extends Object implements Transaction
A minimal `Transaction` for read-only (selecting) queries that run outside a managed transaction. It is not a real transaction - it exists only so that a DataNode has a thread-bound transaction to acquire and release its connection through, and it keeps that connection in autocommit mode (each select commits on its own, which is optimal for reads). It never commits or rolls back, and fires no `TransactionListener` events.

Reads that require a real transaction (e.g. materializing PostgreSQL `oid`/BLOB large objects) are not routed here - they use a `CayenneTransaction`.

Since:
  5.0

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ReadOnlyTransaction()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addListener(TransactionListener listener)`
  `void`
  `begin()`
  Starts a Transaction.
  `void`
  `commit()`
  `Map<String, Connection>`
  `getConnections()`
  Returns all connections associated with the transaction.
  `Connection`
  `getOrCreateConnection(String connectionName, DataSource dataSource)`
  Retrieves a connection for the given symbolic name.
  `boolean`
  `isExternal()`
  Is this transaction managed by external transaction manager
  `boolean`
  `isRollbackOnly()`
  `void`
  `rollback()`
  `void`
  `setRollbackOnly()`

  ### Methods inherited from class Object

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

- ## Constructor Details

  - ### ReadOnlyTransaction

    public ReadOnlyTransaction()

- ## Method Details

  - ### 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`

  - ### commit

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

  - ### rollback

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

  - ### 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.

  - ### begin

    public void begin()
    Description copied from interface: `Transaction`
    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`

  - ### 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`

  - ### isExternal

    public boolean isExternal()
    Description copied from interface: `Transaction`
    Is this transaction managed by external transaction manager
    Specified by:
      `isExternal` in interface `Transaction`