我是Oracle Coherence的新手,我需要在我的示例项目中添加功能,这将启用两个Cache之间的事务管理。
因为我们从NamedCache获取事务对象,并且两个不同的Cache都有两个不同的NamedCache对象。那么我如何能够实现在两个Cache之间实现事务管理的功能。
对于单一缓存,我可以使用以下代码进行事务管理。
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");
}
}
答案 0 :(得分:2)
根据Oracle文档,您可以使用Connection API实现此目的。这两个缓存都应该是transactional,并从exports.addFunction = functions.database
.ref('/Users/{uid}/GyroScope X-axis')
.onWrite(event => {
var add = 0;
const addGyroX = admin.database().ref('/GyroXaddition');
const userRef = event.data.adminRef;
userRef.once('value').then(snapshot => {
snapshot.forEach(childrensnap => {
var reading = childrensnap.key;
var childData = reading.val();
add = add+childData;
});
});
return addGyroX.set(add);
});
类的同一个实例中获取。请参见示例here。
请注意,如果您计划在缓存和数据源之间使用同步,则此功能可能会有所不同,具体取决于您使用的同步策略(直写,后写等)。