【问题标题】:Oracle Coherence : Transaction Management between two CacheOracle Coherence:两个缓存之间的事务管理
【发布时间】:2017-06-30 12:02:32
【问题描述】:

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

【问题讨论】:

    标签: java oracle-coherence


    【解决方案1】:

    根据 Oracle 文档,您可以使用 Connection API 来实现这一点。两个缓存都应该是transactional 并从Connection 类的同一个实例中获得。参见示例here

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

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      相关资源
      最近更新 更多