6

注文管理システムのメモリ内データベースとして infinispan を使用したいと考えています。そこで、次のタイプの操作を行う必要があります。ここで、キャッシュ アカウント キャッシュには、DB からロードされた顧客キャッシュ アカウントが含まれます。現金 account1 の初期残高が 1000 で、cashAccount2 が 2000 であるとします。jboss 7.1 アプリケーション サーバーのトランザクションで両方の現金口座を更新します。結果として期待されるのは、この操作がトランザクション内で発生したため、両方の現金口座の残高が変更されずに残ることです。残念ながら、トランザクションがロールバックされた後でも、キャッシュ内に更新オブジェクトが表示されます。私たちが調べているのは、トランザクションがキャッシュから削除されるロールバック時に、トランザクション内のキャッシュにオブジェクトを追加するときです。ただし、既存のオブジェクトの変更はそのまま残ります。

これは、私たちがやりたいことのほんの一例です。実際には、1 つのトランザクションで複数のオブジェクトを更新する必要があります。

このタイプの操作に infinispan を使用できることをお知らせください。

cashAccountCache= provider.getCacheContainer().getCache(CACHE_NAME);
        try {
            utx.begin();
            CashAccount cashAccount1 = cashAccountCache.get("cashAccNumber1");
            CashAccount cashAccount2 = cashAccountCache.get("cashAccNumber2");
            cashAccount1.setBalance( cashAccount1 .getBalance() + 100);
            cashAccount2.setBalance( cashAccount2 .getBalance() + 200);
             if(true) throw new RuntimeException();
            utx.commit();
        } catch (Exception e) {
            if (utx != null) {
                try {
                    utx.rollback();
                } catch (Exception e1) {
                }
            }
        }
4

1 に答える 1

3

infinispan でこれを行う正しい方法は、CacheAccount オブジェクトを不変にすることです。そうしないと、オブジェクトのプロパティを変更することになり、Infinispan はそれを制御できません。

//CashAccount Class
public class CashAccount{

    public CashAccount setBalance(int balance){
        CacheAccount account = new CacheAccount(this); //deep copy
        account.setBalance(balance);
        return account;
    }

}



cashAccountCache= provider.getCacheContainer().getCache(CACHE_NAME);
try {
    utx.begin();
    CashAccount cashAccount1 = cashAccountCache.get("cashAccNumber1");
    CashAccount cashAccount2 = cashAccountCache.get("cashAccNumber2");
    cashAccount1 = cashAccount1.setBalance( cashAccount1 .getBalance() + 100);
    cashAccount2 = cashAccount2.setBalance( cashAccount2 .getBalance() + 200);
    cacheAccountCache.put("cashAccNumber1", cashAccount1);
    cacheAccountCache.put("cashAccNumber2",cacheAccount2);
    if(true) throw new RuntimeException();
    utx.commit();
} catch (Exception e) {
    if (utx != null) {
        try {
            utx.rollback();
        } catch (Exception e1) {
        }
    }
}
于 2013-11-08T15:38:07.520 に答える