【问题标题】:infinispan cache object update in transaction roll back issue事务回滚问题中的 infinispan 缓存对象更新
【发布时间】:2013-10-31 17:55:02
【问题描述】:

我们希望在订单管理系统中使用 infinispan 作为内存数据库。在那里我们需要做以下类型的操作。这里的现金账户缓存包含从数据库加载的客户缓存账户。假设现金账户 1 的初始余额为 1000,现金账户 2 为 2000。我们在 jboss 7.1 应用服务器的交易中更新两个现金账户。我们期望结果是两个现金账户的余额保持不变,因为此操作发生在交易内部。但不幸的是,即使在事务回滚之后,我们也可以在缓存中看到更新对象。 ut 我们检查的是,当我们将对象添加到事务中的缓存时,当事务回滚时,它将从缓存中删除。但对现有对象的修改保持不变。

这只是我们想要做的一个示例。实际涉及在单个事务中更新多个对象。

能否请您告诉我们可以使用 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) {
                }
            }
        }

【问题讨论】:

  • 也许缓存被配置为非事务性的?默认情况下,缓存是非事务性的。您需要明确地使它们具有事务性(请参阅 AS7 配置文件中的缓存配置)。这似乎是某种设置问题。
  • 你是不是在缓存不知道的情况下直接修改 map 中存储的值?

标签: java jakarta-ee jboss jboss7.x infinispan


【解决方案1】:

在 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) {
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 2019-03-04
    • 2020-09-27
    • 1970-01-01
    • 2014-07-15
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多