org.apache.commons.transaction.memory
Class OptimisticMapWrapper
java.lang.Object
org.apache.commons.transaction.memory.TransactionalMapWrapper
org.apache.commons.transaction.memory.OptimisticMapWrapper
- All Implemented Interfaces:
- java.util.Map, javax.transaction.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 classes/interfaces inherited from interface java.util.Map |
java.util.Map.Entry<K,V> |
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 |
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 |
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 java.util.Set activeTransactions
logger
protected LoggerFacade logger
commitLock
protected ReadWriteLock commitLock
OptimisticMapWrapper
public OptimisticMapWrapper(java.util.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(java.util.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 wrappedmapFactory
- factory for temporary mapssetFactory
- factory for temporary sets
OptimisticMapWrapper
public OptimisticMapWrapper(java.util.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 wrappedmapFactory
- factory for temporary mapssetFactory
- factory for temporary setslogger
- generic logger used for all kinds of logging
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 java.lang.Object checkForConflicts()
copyChangesToConcurrentTransactions
protected void copyChangesToConcurrentTransactions()
Copyright ? 2004 The Apache Software Foundation. All Rights Reserved.