org.apache.commons.transaction.memory
Class OptimisticMapWrapper

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

public class OptimisticMapWrapper
extends TransactionalMapWrapper

Wrapper that adds transactional control to all kinds of maps that implement the Map interface. By using a naive optimistic transaction control this wrapper has better isolation than TransactionalMapWrapper, but may also fail to commit.
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 TransactionalMapWrapper.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 TransactionalMapWrapper.put(Object, Object).
Note: This wrapper guarantees isolation level SERIALIZABLE.
Caution: This implementation might be slow when large amounts of data is changed in a transaction as much references will need to be copied around.

Version:
$Revision$
See Also:
TransactionalMapWrapper, PessimisticMapWrapper

Nested Class Summary
 class OptimisticMapWrapper.CopyingTxContext
           
 
Nested classes/interfaces inherited from class org.apache.commons.transaction.memory.TransactionalMapWrapper
TransactionalMapWrapper.HashEntry, TransactionalMapWrapper.TxContext
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected static int ACCESS_TIMEOUT
           
protected  Set activeTransactions
           
protected static int COMMIT_TIMEOUT
           
protected  ReadWriteLock commitLock
           
protected  LoggerFacade logger
           
 
Fields inherited from class org.apache.commons.transaction.memory.TransactionalMapWrapper
mapFactory, setFactory, 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
OptimisticMapWrapper(Map wrapped)
          Creates a new optimistic transactional map wrapper.
OptimisticMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory)
          Creates a new optimistic transactional map wrapper.
OptimisticMapWrapper(Map wrapped, MapFactory mapFactory, SetFactory setFactory, LoggerFacade logger)
          Creates a new optimistic transactional map wrapper.
 
Method Summary
 Object checkForConflicts()
           
 void commitTransaction()
          Commits all changes made in the current transaction and deletes the association between the current thread and the transaction.
 void commitTransaction(boolean force)
           
protected  void copyChangesToConcurrentTransactions()
           
 void rollbackTransaction()
          Discards all changes made in the current transaction and deletes the association between the current thread and the transaction.
 void startTransaction()
          Starts a new transaction and associates it with the current thread.
 
Methods inherited from class org.apache.commons.transaction.memory.TransactionalMapWrapper
clear, containsKey, containsValue, entrySet, get, getActiveTx, getTransactionState, isEmpty, isReadOnly, isTransactionMarkedForRollback, keySet, markTransactionForRollback, put, putAll, remove, resumeTransaction, setActiveTx, size, suspendTransaction, 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

COMMIT_TIMEOUT

protected static final int COMMIT_TIMEOUT
See Also:
Constant Field Values

ACCESS_TIMEOUT

protected static final int ACCESS_TIMEOUT
See Also:
Constant Field Values

activeTransactions

protected Set activeTransactions

logger

protected LoggerFacade logger

commitLock

protected ReadWriteLock commitLock
Constructor Detail

OptimisticMapWrapper

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

Parameters:
wrapped - map to be wrapped

OptimisticMapWrapper

public OptimisticMapWrapper(Map wrapped,
                            MapFactory mapFactory,
                            SetFactory setFactory)
Creates a new optimistic 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

OptimisticMapWrapper

public OptimisticMapWrapper(Map wrapped,
                            MapFactory mapFactory,
                            SetFactory setFactory,
                            LoggerFacade logger)
Creates a new optimistic 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
logger - generic logger used for all kinds of logging
Method Detail

startTransaction

public void startTransaction()
Description copied from class: TransactionalMapWrapper
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 TransactionalMapWrapper.commitTransaction() is called. Use TransactionalMapWrapper.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.

Overrides:
startTransaction in class TransactionalMapWrapper
See Also:
TransactionalMapWrapper.commitTransaction(), TransactionalMapWrapper.rollbackTransaction()

rollbackTransaction

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

Overrides:
rollbackTransaction in class TransactionalMapWrapper
See Also:
TransactionalMapWrapper.startTransaction(), TransactionalMapWrapper.commitTransaction()

commitTransaction

public void commitTransaction()
                       throws ConflictException
Description copied from class: TransactionalMapWrapper
Commits all changes made in the current transaction and deletes the association between the current thread and the transaction.

Overrides:
commitTransaction in class TransactionalMapWrapper
Throws:
ConflictException
See Also:
TransactionalMapWrapper.startTransaction(), TransactionalMapWrapper.rollbackTransaction()

commitTransaction

public void commitTransaction(boolean force)
                       throws ConflictException
Throws:
ConflictException

checkForConflicts

public Object checkForConflicts()

copyChangesToConcurrentTransactions

protected void copyChangesToConcurrentTransactions()


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