public class TransactionAwareProxyFactory<T> extends Object
Factory for transaction aware objects (like lists, sets, maps). If a transaction is active when a method is called on an instance created by the factory, it makes a copy of the target object and carries out all operations on the copy. Only when the transaction commits is the target re-initialised with the copy.
Works well with collections and maps for testing transactional behaviour
without needing a database. The base implementation handles lists, sets and
maps. Subclasses can implement begin(Object)
and
commit(Object, Object)
to provide support for other resources.
Generally not intended for multi-threaded use, but the
append only version
of
collections gives isolation between threads operating on different keys in a
map, provided they only append to the map. (Threads are limited to removing
entries that were created in the same transaction.)
Modifier and Type | Method and Description |
---|---|
protected T |
begin(T target)
Make a copy of the target that can be used inside a transaction to
isolate changes from the original.
|
protected void |
commit(T copy,
T target)
Take the working copy state and commit it back to the original target.
|
static <T> List<T> |
createAppendOnlyTransactionalList() |
static <K,V> ConcurrentMap<K,V> |
createAppendOnlyTransactionalMap() |
static <T> Set<T> |
createAppendOnlyTransactionalSet() |
static <T> List<T> |
createTransactionalList() |
static <T> List<T> |
createTransactionalList(List<T> list) |
static <K,V> Map<K,V> |
createTransactionalMap() |
static <K,V> Map<K,V> |
createTransactionalMap(Map<K,V> map) |
static <T> Set<T> |
createTransactionalSet() |
static <T> Set<T> |
createTransactionalSet(Set<T> set) |
protected final T begin(T target)
target
- the target object (List, Set or Map)protected void commit(T copy, T target)
copy
- the working copy.target
- the original target of the factory.public static <K,V> Map<K,V> createTransactionalMap()
public static <K,V> ConcurrentMap<K,V> createAppendOnlyTransactionalMap()
public static <T> Set<T> createAppendOnlyTransactionalSet()
public static <T> Set<T> createTransactionalSet()
public static <T> List<T> createAppendOnlyTransactionalList()
public static <T> List<T> createTransactionalList()
Copyright © 2014 Pivotal. All rights reserved.