org.apache.commons.transaction.memory
Class OptimisticTxMap<K,V>

java.lang.Object
  extended by org.apache.commons.transaction.AbstractTransactionalResourceManager<BasicTxMap.MapTxContext>
      extended by org.apache.commons.transaction.memory.BasicTxMap<K,V>
          extended by org.apache.commons.transaction.memory.OptimisticTxMap<K,V>
All Implemented Interfaces:
Map<K,V>, ManageableResourceManager, TxMap<K,V>, TransactionalResourceManager

public class OptimisticTxMap<K,V>
extends BasicTxMap<K,V>
implements TxMap<K,V>

Map featuring transactional control.

By using a naive optimistic transaction control this implementation has better isolation than BasicTxMap, but may also fail to commit.

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.

This implementation wraps a map of type ConcurrentHashMap.

This implementation is thread-safe.

See Also:
BasicTxMap, PessimisticTxMap, ConcurrentHashMap

Nested Class Summary
 class OptimisticTxMap.CopyingTxContext
           
 
Nested classes/interfaces inherited from class org.apache.commons.transaction.memory.BasicTxMap
BasicTxMap.HashEntry, BasicTxMap.MapTxContext
 
Nested classes/interfaces inherited from class org.apache.commons.transaction.AbstractTransactionalResourceManager
AbstractTransactionalResourceManager.AbstractTxContext
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
 
Fields inherited from class org.apache.commons.transaction.memory.BasicTxMap
wrapped
 
Fields inherited from class org.apache.commons.transaction.AbstractTransactionalResourceManager
activeTx
 
Constructor Summary
OptimisticTxMap(String name)
           
OptimisticTxMap(String name, LockManager<Object,Object> lm)
           
 
Method Summary
 Object checkForConflicts()
           
 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.
 boolean commitTransaction(boolean force)
           
protected  void copyChangesToConcurrentTransactions()
           
protected  OptimisticTxMap.CopyingTxContext createContext()
           
 long getAccessTimeout()
           
 long getCommitTimeout()
           
 void rollbackTransaction()
          Discards all changes made in the current transaction and deletes the association between the current thread and the transaction.
 void setAccessTimeout(long accessTimeout)
           
 void setCommitTimeout(long commitTimeout)
           
 void startTransaction(long timeout, TimeUnit unit)
          Starts a new transaction and associates it with the current thread.
 
Methods inherited from class org.apache.commons.transaction.memory.BasicTxMap
clear, containsKey, containsValue, entrySet, get, getActiveTx, getWrappedMap, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class org.apache.commons.transaction.AbstractTransactionalResourceManager
forgetTransaction, getCheckedActiveTx, getLm, getName, isPartofComplexTransaction, isReadOnly, isRollbackOnly, joinTransaction, prepareTransaction, setActiveTx, setLm, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.commons.transaction.memory.TxMap
getWrappedMap
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from interface org.apache.commons.transaction.ManageableResourceManager
forgetTransaction, isReadOnly, isRollbackOnly, joinTransaction, prepareTransaction
 

Constructor Detail

OptimisticTxMap

public OptimisticTxMap(String name)

OptimisticTxMap

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

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
Overrides:
startTransaction in class AbstractTransactionalResourceManager<BasicTxMap.MapTxContext>
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
Overrides:
rollbackTransaction in class AbstractTransactionalResourceManager<BasicTxMap.MapTxContext>
See Also:
TransactionalResourceManager.startTransaction(long, TimeUnit), TransactionalResourceManager.commitTransaction()

commitTransaction

public boolean commitTransaction()
                          throws LockException
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
Overrides:
commitTransaction in class AbstractTransactionalResourceManager<BasicTxMap.MapTxContext>
Throws:
LockException
See Also:
TransactionalResourceManager.startTransaction(long, TimeUnit), TransactionalResourceManager.rollbackTransaction()

commitTransaction

public boolean commitTransaction(boolean force)
                          throws LockException
Throws:
LockException

checkForConflicts

public Object checkForConflicts()

copyChangesToConcurrentTransactions

protected void copyChangesToConcurrentTransactions()

createContext

protected OptimisticTxMap.CopyingTxContext createContext()
Overrides:
createContext in class BasicTxMap<K,V>

getAccessTimeout

public long getAccessTimeout()

setAccessTimeout

public void setAccessTimeout(long accessTimeout)

getCommitTimeout

public long getCommitTimeout()

setCommitTimeout

public void setCommitTimeout(long commitTimeout)

commitCanFail

public 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
Overrides:
commitCanFail in class BasicTxMap<K,V>
Returns:
true if a commit could fail


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