Cha*_*oda 6 java jboss java-ee infinispan jboss7.x
我们希望将infinispan用作订单管理系统中的内存数据库.我们需要做以下类型的操作.现金帐户缓存包含从DB加载的客户缓存帐户.假设现金账户1的初始余额为1000,cashAccount2为2000.我们在jboss 7.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) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 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) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1072 次 |
最近记录: |