org.sormula.active
Class ActiveTransaction

java.lang.Object
  extended by org.sormula.active.ActiveTransaction

public class ActiveTransaction
extends java.lang.Object

A class to group multiple active record operations within one JDBC transaction.

    ActiveTransaction transaction = new ActiveTransaction(new ActiveDatabase(dataSource));

    try
    {
        transaction.begin();

        // ...some active operations...

        transaction.commit();
    }
    catch (ActiveException e)
    {
        transaction.rollback();
    }
 
Only one instance of an ActiveTransaction may be in use for an instance of an ActiveDatabase at one time. Create one instance of an ActiveDatabase for each possible transaction that may occur at that same time.

Since:
1.7.1 and 2.1.1
Author:
Jeff Miller

Constructor Summary
ActiveTransaction()
          Constructs for the default active database.
ActiveTransaction(ActiveDatabase activeDatabase)
          Constructs an ActiveDatabase.
 
Method Summary
 void begin()
          Starts the transaction.
protected  void close()
          Cleans up resources that are used by this transaction.
 void commit()
          Completes the transaction.
 ActiveDatabase getActiveDatabase()
          Gets the active database associated with this transaction.
 OperationTransaction getOperationTransaction()
          Gets the operation transaction created by this transaction.
 void rollback()
          Rolls back the transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveTransaction

public ActiveTransaction()
                  throws ActiveException
Constructs for the default active database. The default active database must be set with ActiveDatabase.setDefault(ActiveDatabase) prior to using this constructor.

Use this constructor only if no other thread may be using it at the same time. This is the case since there is only one default ActiveDatabase per classloader and only one transaction may be in use per ActiveDatabase.

Throws:
ActiveException - if error

ActiveTransaction

public ActiveTransaction(ActiveDatabase activeDatabase)
                  throws ActiveException
Constructs an ActiveDatabase.

Parameters:
activeDatabase - active database associated with transaction
Throws:
ActiveException - if error
Method Detail

getActiveDatabase

public ActiveDatabase getActiveDatabase()
Gets the active database associated with this transaction. Either the default if ActiveTransaction() was used or the one supplied in ActiveTransaction(ActiveDatabase).

Returns:
active database

getOperationTransaction

public OperationTransaction getOperationTransaction()
Gets the operation transaction created by this transaction. The method is typically only used by classes in org.sormula.active.operation package.

Returns:
operation transaction

begin

public void begin()
           throws ActiveException
Starts the transaction. Use commit() or rollback() to close the transaction. This method invokes ActiveDatabase.setActiveTransaction(ActiveTransaction) to indicate that this transaction is in use for the active database.

Throws:
ActiveException - if error

commit

public void commit()
            throws ActiveException
Completes the transaction. Invokes Connection.commit() on the underlying JDBC connection. This method invokes ActiveDatabase.setActiveTransaction(ActiveTransaction) with a null parameter to indicate that no transaction is in use for the active database.

Throws:
ActiveException - if error

rollback

public void rollback()
              throws ActiveException
Rolls back the transaction. Invokes Connection.rollback() on the underlying JDBC connection. This method invokes ActiveDatabase.setActiveTransaction(ActiveTransaction) with a null parameter to indicate that no transaction is in use for the active database.

Throws:
ActiveException - if error

close

protected void close()
              throws ActiveException
Cleans up resources that are used by this transaction. Specifically, the active transaction is removed from active database and the JDBC connection is closed.

Throws:
ActiveException - if error