org.apache.commons.transaction
Class AbstractTransactionalResourceManager<T extends AbstractTransactionalResourceManager.AbstractTxContext>

java.lang.Object
  extended by org.apache.commons.transaction.AbstractTransactionalResourceManager<T>
All Implemented Interfaces:
ManageableResourceManager, TransactionalResourceManager
Direct Known Subclasses:
BasicTxMap, TxFileResourceManager

public abstract class AbstractTransactionalResourceManager<T extends AbstractTransactionalResourceManager.AbstractTxContext>
extends Object
implements ManageableResourceManager

Abstract base class for transactional resource managers.

This implementation takes care of most administrative tasks a transactional resource manager has to perform. Sublcass AbstractTransactionalResourceManager.AbstractTxContext to hold all information necessary for each transaction. Additionally, you have to implement createContext() to create an object of that type, and commitCanFail().

This implementation is thread-safe.


Nested Class Summary
 class AbstractTransactionalResourceManager.AbstractTxContext
           
 
Field Summary
protected  ThreadLocal<T> activeTx
           
 
Constructor Summary
AbstractTransactionalResourceManager(String name)
           
AbstractTransactionalResourceManager(String name, LockManager<Object,Object> lm)
           
 
Method Summary
abstract  boolean commitCanFail()
          Checks whether a tried commit could possibly fail because of logical reasons.
 boolean commitTransaction()
          Commits all changes made in the current transaction and deletes the association between the current thread and the transaction.
protected abstract  T createContext()
           
 void forgetTransaction()
          Instructs the resource manager to forget about the current transaction.
protected  T getActiveTx()
           
protected  T getCheckedActiveTx()
           
protected  LockManager<Object,Object> getLm()
           
 String getName()
           
 boolean isPartofComplexTransaction()
           
 boolean isReadOnly()
          Checks if there had been any write operations on this resource manager since the start of the transaction.
 boolean isRollbackOnly()
          Checks whether this resource manager allows a rollback as the only valid outcome.
 void joinTransaction(LockManager<Object,Object> lm)
          Lets this resource manager join a transaction that is protected by a common lock manager.
 boolean prepareTransaction()
          Checks whether this resource manager is willing and able to commit its part of the complex transaction.
 void rollbackTransaction()
          Discards all changes made in the current transaction and deletes the association between the current thread and the transaction.
protected  void setActiveTx(T txContext)
           
 void setLm(LockManager<Object,Object> lm)
           
 void setName(String name)
           
 void startTransaction(long timeout, TimeUnit unit)
          Starts a new transaction and associates it with the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activeTx

protected ThreadLocal<T extends AbstractTransactionalResourceManager.AbstractTxContext> activeTx
Constructor Detail

AbstractTransactionalResourceManager

public AbstractTransactionalResourceManager(String name)

AbstractTransactionalResourceManager

public AbstractTransactionalResourceManager(String name,
                                            LockManager<Object,Object> lm)
Method Detail

createContext

protected abstract T createContext()

isRollbackOnly

public boolean isRollbackOnly()
Description copied from interface: ManageableResourceManager
Checks whether this resource manager 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 ManageableResourceManager
Returns:
true if this resource manager can only roll back

startTransaction

public void startTransaction(long timeout,
                             TimeUnit unit)
Description copied from interface: TransactionalResourceManager
Starts a new transaction and associates it with the current thread. All subsequent changes in the same thread made to the resource manager are invisible from other threads until TransactionalResourceManager.commitTransaction() is called. Use TransactionalResourceManager.rollbackTransaction() to discard your changes. After calling either method there will be no transaction associated to the current thread any longer.

Caution: Be careful to finally call one of those methods, as otherwise the transaction will lurk around for ever.

Specified by:
startTransaction in interface TransactionalResourceManager
See Also:
TransactionalResourceManager.commitTransaction(), TransactionalResourceManager.rollbackTransaction()

rollbackTransaction

public void rollbackTransaction()
Description copied from interface: TransactionalResourceManager
Discards all changes made in the current transaction and deletes the association between the current thread and the transaction.

Specified by:
rollbackTransaction in interface TransactionalResourceManager
See Also:
TransactionalResourceManager.startTransaction(long, TimeUnit), TransactionalResourceManager.commitTransaction()

forgetTransaction

public void forgetTransaction()
Description copied from interface: ManageableResourceManager
Instructs the resource manager to forget about the current transaction.

Specified by:
forgetTransaction in interface ManageableResourceManager

commitTransaction

public boolean commitTransaction()
Description copied from interface: TransactionalResourceManager
Commits all changes made in the current transaction and deletes the association between the current thread and the transaction.

Specified by:
commitTransaction in interface TransactionalResourceManager
See Also:
TransactionalResourceManager.startTransaction(long, TimeUnit), TransactionalResourceManager.rollbackTransaction()

getActiveTx

protected T getActiveTx()

getCheckedActiveTx

protected T getCheckedActiveTx()

setActiveTx

protected void setActiveTx(T txContext)

isReadOnly

public boolean isReadOnly()
Description copied from interface: ManageableResourceManager
Checks if there had been any write operations on this resource manager since the start of the transaction. If not the transaction is not required to call either TransactionalResourceManager.commitTransaction() or TransactionalResourceManager.rollbackTransaction(), but only ManageableResourceManager.forgetTransaction().

Specified by:
isReadOnly in interface ManageableResourceManager
Returns:
true if there had been read operations only

getLm

protected LockManager<Object,Object> getLm()

setLm

public void setLm(LockManager<Object,Object> lm)

getName

public String getName()

setName

public void setName(String name)

commitCanFail

public abstract boolean commitCanFail()
Description copied from interface: ManageableResourceManager
Checks whether a tried commit could possibly fail because of logical reasons.

Specified by:
commitCanFail in interface ManageableResourceManager
Returns:
true if a commit could fail

joinTransaction

public void joinTransaction(LockManager<Object,Object> lm)
Description copied from interface: ManageableResourceManager
Lets this resource manager join a transaction that is protected by a common lock manager. An implementation is required to perform all locking operations on the given lock manager as long as it takes part in the complex transaction.

Specified by:
joinTransaction in interface ManageableResourceManager
Parameters:
lm - the common lock maanger

prepareTransaction

public boolean prepareTransaction()
Description copied from interface: ManageableResourceManager
Checks whether this resource manager is willing and able to commit its part of the complex transaction.

Specified by:
prepareTransaction in interface ManageableResourceManager
Returns:
true if this resource manager can commit its part of the transaction

isPartofComplexTransaction

public boolean isPartofComplexTransaction()


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