org.apache.commons.transaction.memory
Class TransactionalMapWrapper

java.lang.Object
  extended by org.apache.commons.transaction.memory.TransactionalMapWrapper
All Implemented Interfaces:
Map, Status
Direct Known Subclasses:
OptimisticMapWrapper, PessimisticMapWrapper

public class TransactionalMapWrapper
extends Object
implements Map, Status

Wrapper that adds transactional control to all kinds of maps that implement the Map interface. This wrapper has rather weak isolation, but is simply, neven blocks and commits will never fail for logical reasons.
Start a transaction by calling startTransaction(). Then perform the normal actions on the map and finally either call commitTransaction() to make your changes permanent or rollbackTransaction() to undo them.
Caution: Do not modify values retrieved by get(Object) as this will circumvent the transactional mechanism. Rather clone the value or copy it in a way you see fit and store it back using put(Object, Object).
Note: This wrapper guarantees isolation level READ COMMITTED only. I.e. as soon a value is committed in one transaction it will be immediately visible in all other concurrent transactions.

Version:
$Id: TransactionalMapWrapper.java 493628 2007-01-07 01:42:48Z joerg $
See Also:
OptimisticMapWrapper, PessimisticMapWrapper

Nested Class Summary
protected static class TransactionalMapWrapper.HashEntry
           
 class TransactionalMapWrapper.TxContext
           
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry
 
Field Summary
protected  MapFactory mapFactory
          Factory to be used to create temporary maps for transactions.
protected  SetFactory setFactory
          Factory to be used to create temporary sets for transactions.
protected  Map wrapped
          The map wrapped.
 
Fields inherited from interface javax.transaction.Status
STATUS_ACTIVE, STATUS_COMMITTED, STATUS_COMMITTING, STATUS_MARKED_ROLLBACK, STATUS_NO_TRANSACTION, STATUS_PREPARED, STATUS_PREPARING, STATUS_ROLLEDBACK, STATUS_ROLLING_BACK, STATUS_UNKNOWN
 
Constructor Summary
TransactionalMapWrapper(Map wrapped)
          Creates a new transactional map wrapper.
TransactionalMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory)
          Creates a new transactional map wrapper.
 
Method Summary
 void clear()
           
 void commitTransaction()
          Commits all changes made in the current transaction and deletes the association between the current thread and the transaction.
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set entrySet()
           
 Object get(Object key)
           
protected  TransactionalMapWrapper.TxContext getActiveTx()
           
 int getTransactionState()
          Returns the state of the current transaction.
 boolean isEmpty()
           
 boolean isReadOnly()
          Checks if any write operations have been performed inside this transaction.
 boolean isTransactionMarkedForRollback()
          Checks whether this transaction has been marked to allow a rollback as the only valid outcome.
 Set keySet()
           
 void markTransactionForRollback()
          Marks the current transaction to allow only a rollback as valid outcome.
 Object put(Object key, Object value)
           
 void putAll(Map map)
           
 Object remove(Object key)
           
 void resumeTransaction(TransactionalMapWrapper.TxContext suspendedTx)
          Resumes a transaction in the current thread that has previously been suspened by suspendTransaction().
 void rollbackTransaction()
          Discards all changes made in the current transaction and deletes the association between the current thread and the transaction.
protected  void setActiveTx(TransactionalMapWrapper.TxContext txContext)
           
 int size()
           
 void startTransaction()
          Starts a new transaction and associates it with the current thread.
 TransactionalMapWrapper.TxContext suspendTransaction()
          Suspends the transaction associated to the current thread.
 Collection values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

wrapped

protected Map wrapped
The map wrapped.


mapFactory

protected MapFactory mapFactory
Factory to be used to create temporary maps for transactions.


setFactory

protected SetFactory setFactory
Factory to be used to create temporary sets for transactions.

Constructor Detail

TransactionalMapWrapper

public TransactionalMapWrapper(Map wrapped)
Creates a new transactional map wrapper. Temporary maps and sets to store transactional data will be instances of HashMap and HashSet.

Parameters:
wrapped - map to be wrapped

TransactionalMapWrapper

public TransactionalMapWrapper(Map wrapped,
                               MapFactory mapFactory,
                               SetFactory setFactory)
Creates a new transactional map wrapper. Temporary maps and sets to store transactional data will be created and disposed using MapFactory and SetFactory.

Parameters:
wrapped - map to be wrapped
mapFactory - factory for temporary maps
setFactory - factory for temporary sets
Method Detail

isReadOnly

public boolean isReadOnly()
Checks if any write operations have been performed inside this transaction.

Returns:
true if no write opertation has been performed inside the current transaction, false otherwise

isTransactionMarkedForRollback

public boolean isTransactionMarkedForRollback()
Checks whether this transaction has been marked to allow a rollback as the only valid outcome. This can be set my method markTransactionForRollback() or might be set internally be any fatal error. 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, also rolled back.

Returns:
true if this transaction has been marked for a roll back
See Also:
markTransactionForRollback()

markTransactionForRollback

public void markTransactionForRollback()
Marks the current transaction to allow only a rollback as valid outcome.

See Also:
isTransactionMarkedForRollback()

suspendTransaction

public TransactionalMapWrapper.TxContext suspendTransaction()
Suspends the transaction associated to the current thread. I.e. the associated between the current thread and the transaction is deleted. This is useful when you want to continue the transaction in another thread later. Call resumeTransaction(TxContext) - possibly in another thread than the current - to resume work on the transaction.

Caution: When calling this method the returned identifier for the transaction is the only remaining reference to the transaction, so be sure to remember it or the transaction will be eventually deleted (and thereby rolled back) as garbage.

Returns:
an identifier for the suspended transaction, will be needed to later resume the transaction by resumeTransaction(TxContext)
See Also:
resumeTransaction(TxContext)

resumeTransaction

public void resumeTransaction(TransactionalMapWrapper.TxContext suspendedTx)
Resumes a transaction in the current thread that has previously been suspened by suspendTransaction().

Parameters:
suspendedTx - the identifier for the transaction to be resumed, delivered by suspendTransaction()
See Also:
suspendTransaction()

getTransactionState

public int getTransactionState()
Returns the state of the current transaction.

Returns:
state of the current transaction as decribed in the Status interface.

startTransaction

public void startTransaction()
Starts a new transaction and associates it with the current thread. All subsequent changes in the same thread made to the map are invisible from other threads until commitTransaction() is called. Use 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.

See Also:
commitTransaction(), rollbackTransaction()

rollbackTransaction

public void rollbackTransaction()
Discards all changes made in the current transaction and deletes the association between the current thread and the transaction.

See Also:
startTransaction(), commitTransaction()

commitTransaction

public void commitTransaction()
Commits all changes made in the current transaction and deletes the association between the current thread and the transaction.

See Also:
startTransaction(), rollbackTransaction()

clear

public void clear()
Specified by:
clear in interface Map
See Also:
Map.clear()

size

public int size()
Specified by:
size in interface Map
See Also:
Map.size()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map
See Also:
Map.isEmpty()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map
See Also:
Map.containsValue(java.lang.Object)

values

public Collection values()
Specified by:
values in interface Map
See Also:
Map.values()

putAll

public void putAll(Map map)
Specified by:
putAll in interface Map
See Also:
Map.putAll(java.util.Map)

entrySet

public Set entrySet()
Specified by:
entrySet in interface Map
See Also:
Map.entrySet()

keySet

public Set keySet()
Specified by:
keySet in interface Map
See Also:
Map.keySet()

get

public Object get(Object key)
Specified by:
get in interface Map
See Also:
Map.get(java.lang.Object)

remove

public Object remove(Object key)
Specified by:
remove in interface Map
See Also:
Map.remove(java.lang.Object)

put

public Object put(Object key,
                  Object value)
Specified by:
put in interface Map
See Also:
Map.put(java.lang.Object, java.lang.Object)

getActiveTx

protected TransactionalMapWrapper.TxContext getActiveTx()

setActiveTx

protected void setActiveTx(TransactionalMapWrapper.TxContext txContext)


Copyright © 2004-2009 Apache Software Foundation. All Rights Reserved.