Oracle Coherence:两个缓存之间的事务管理

Oracle Coherence : Transaction Management between two Cache

我是 Oracle Coherence 的新手,我需要在我的示例项目中添加功能,这将启用两个缓存之间的事务管理。

正如我们从NamedCache中获取事务对象一样,对于不同的Cache,有两个不同的NamedCache对象。那么我如何能够实现在两个缓存之间启用事务管理的功能。

对于单缓存,我可以使用以下代码进行事务管理。

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");
    }
}

根据 Oracle 文档,您可以使用连接 API 来实现此目的。两个缓存都应该是 transactional and obtained from the same instance of Connection class. See example here.

请注意,如果您计划在缓存和数据源之间使用同步,则此功能的工作方式可能会有所不同,具体取决于您使用的同步策略(直写、后写等)。