I am new in Oracle Coherence, I need to add functionality in my sample project which will enable transaction management between two Cache.
As we would getting the transaction object from NamedCache, and there are two different NamedCache object for both different Cache. So how i am able to achieved functionality which enable transaction management between two Cache.
For Single Cache i am able to do transaction management with following code.
public class TransactionExample extends Base {
public static void main(String[] args) {
// populate the cache
NamedCache cache = CacheFactory.getCache("dist-extend");
cache.clear();
String key1 = "key1";
String key2 = "key2";
//cache.clear();
// create one TransactionMap per NamedCache
TransactionMap mapTx = CacheFactory.getLocalTransaction(cache);
mapTx.setTransactionIsolation(TransactionMap.TRANSACTION_REPEATABLE_GET);
mapTx.setConcurrency(TransactionMap.CONCUR_PESSIMISTIC);
// gather the cache(s) into a Collection
Collection txnCollection = java.util.Collections.singleton(mapTx);
boolean fTxSucceeded = false;
try {
// start the transaction
mapTx.begin();
mapTx.put(key1, new Integer(1001));
//generateException()
mapTx.put(key2, new Integer(2001));
// commit the changes
fTxSucceeded = CacheFactory.commitTransactionCollection(txnCollection, 1);
int v1 = ((Integer) cache.get(key1)).intValue();
int v2 = ((Integer) cache.get(key2)).intValue();
out("Transaction " + (fTxSucceeded ? "succeeded" : "did not succeed"));
out("After Insert into Tx Object Updated value for key 1: " + v1);
out("After Insert into Tx Object Updated value for key 2: " + v2);
//CacheFactory.shutdown();
}
catch (Exception t) {
// rollback
CacheFactory.rollbackTransactionCollection(txnCollection);
t.printStackTrace();
}
/*azzert(v1 == 2, "Expected value for key1 == 2");
azzert(v2 == 2, "Expected value for key2 == 2");*/
//
out("Updated Value From Cache key 1: " + cache.get(key1));
out("Updated Value From Cache key 2: " + cache.get(key2));
}
public static void generateException() throws Exception
{
throw new Exception("Manual Error Throw");
}
}
According to Oracle documentation, you can achieve this by using Connection API. Both caches should be transactional and obtained from the same instance of Connection
class. See example here.
Note that if you're planning to use synchronization between the cache and data source, this functionality could work differently depending on synchronization strategy (write-through, write-behind, etc) you use.