org.apache.commons.transaction
Class DefaultTransaction

java.lang.Object
  extended by org.apache.commons.transaction.DefaultTransaction
All Implemented Interfaces:
Transaction

public class DefaultTransaction
extends Object
implements Transaction

Default implementation for the Transaction interface. Needs a common lock manager shared by all resource managers to make detection of distributed deadlocks possible.

Sample usage:


 LockManager<Object, Object> lm = new RWLockManager<Object, Object>();
 Transaction t = new DefaultTransaction(lm);
 TxMap<String, Object> txMap1 = new PessimisticTxMap<String, Object>("TxMap1");
 t.enlistResourceManager(txMap1);
 TxMap<String, Object> txMap2 = new PessimisticTxMap<String, Object>("TxMap2");
 t.enlistResourceManager(txMap2);
 
 try {
     t.start(60, TimeUnit.SECONDS);
     txMap1.put("Olli", "Huhu");
     txMap2.put("Olli", "Haha");
     t.commit();
 } catch (Throwable throwable) {
     t.rollback();
 }
 

This implementation is thread-safe.


Field Summary
protected  LockManager<Object,Object> lm
           
protected  List<ManageableResourceManager> rms
           
protected  boolean started
           
 
Constructor Summary
DefaultTransaction()
          Creates a new transaction implementation using the default lock manager.
DefaultTransaction(LockManager<Object,Object> lm)
          Creates a new transaction implementation.
 
Method Summary
 void commit()
          Commits the complex transaction meaning that all changes made to participating resource managers are made permanent.
 void enlistResourceManager(ManageableResourceManager resourceManager)
          Adds a resource manager to this complex transaction.
 boolean isRollbackOnly()
          Checks whether this transaction allows a rollback as the only valid outcome.
protected  boolean prepare()
           
 void rollback()
          Rolls back the complex transaction meaning that all changes made to participating resource managers are undone.
 void start(long timeout, TimeUnit unit)
          Starts a new transactions having a specific timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lm

protected LockManager<Object,Object> lm

started

protected boolean started

rms

protected List<ManageableResourceManager> rms
Constructor Detail

DefaultTransaction

public DefaultTransaction(LockManager<Object,Object> lm)
Creates a new transaction implementation.

Parameters:
lm - the lock manager shared by all resource managers

DefaultTransaction

public DefaultTransaction()
Creates a new transaction implementation using the default lock manager.

Method Detail

commit

public void commit()
            throws TransactionException
Description copied from interface: Transaction
Commits the complex transaction meaning that all changes made to participating resource managers are made permanent.

Specified by:
commit in interface Transaction
Throws:
TransactionException

enlistResourceManager

public void enlistResourceManager(ManageableResourceManager resourceManager)
Description copied from interface: Transaction
Adds a resource manager to this complex transaction. This means the resource manager will from now on be controlled by this transaction. Access to transactional methods is not allowed until the complex transaction has finished. Of course, it is legal to call the other methods if this manager to perform some real work.

Specified by:
enlistResourceManager in interface Transaction
Parameters:
resourceManager - the resource manager to add

isRollbackOnly

public boolean isRollbackOnly()
Description copied from interface: Transaction
Checks whether this transaction allows a rollback as the only valid outcome. Once a transaction is marked for rollback there is no way to undo this. A transaction that is marked for rollback can not be committed.

Specified by:
isRollbackOnly in interface Transaction
Returns:
true if this transaction can only rolled back

rollback

public void rollback()
Description copied from interface: Transaction
Rolls back the complex transaction meaning that all changes made to participating resource managers are undone.

Specified by:
rollback in interface Transaction

start

public void start(long timeout,
                  TimeUnit unit)
Description copied from interface: Transaction
Starts a new transactions having a specific timeout. You can add resource managers before start or afterwards.

Specified by:
start in interface Transaction
Parameters:
timeout - the maximum time this transaction can run before it times out
unit - the time unit of the timeout argument

prepare

protected boolean prepare()


Copyright © 2004-2007 The Apache Software Foundation. All Rights Reserved.