Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

infinispan cache object update in transaction roll back issue

we hope to use infinispan as a in memory data base in the order management system. There we need to do following type of operation. Here cash account cache contain customer cache account loaded from DB. Say initial balance of the cash account1 is 1000 and cashAccount2 is 2000. we update both cash account in a transaction in a jboss 7.1 application server. what we expect as a result is balances of the both cash account remain without changing since this operation occurred inside the transaction. But unfortunately it even after transaction roll back we can see the update object in side the cache. ut what we examine is when we add a object to the cache in side a transaction when the transaction roll back it will remove from the cache. But modification of the existing object remain as it is.

This is just a sample what we want to do. Actual one involve updating several object in a single transaction.

Could you please let us know it is possible to use infinispan for this type of opperation.

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) {
                }
            }
        }
like image 614
Chaminda Hettigoda Avatar asked Oct 22 '13 17:10

Chaminda Hettigoda


People also ask

How do I clear infinispan cache?

the clear() goes to all nodes. You can use the CACHE_MODE_LOCAL flag to force it to the local node. example: cache. getAdvancedCache().

How does infinispan cache work?

Infinispan caches provide flexible, in-memory data stores that you can configure to suit use cases such as: Boosting application performance with high-speed local caches. Optimizing databases by decreasing the volume of write operations. Providing resiliency and durability for consistent data across clusters.

What is infinispan server?

Infinispan is an open-source in-memory data grid that offers flexible deployment options and robust capabilities for storing, managing, and processing data. Infinispan provides a key/value data store that can hold all types of data, from Java objects to plain text.


1 Answers

The right way to do this in infinispan is to make to CacheAccount object immutable. Otherwise you are changing the property of an object, and Infinispan has no control of it.

//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) {
        }
    }
}
like image 160
Changgeng Avatar answered Oct 28 '22 08:10

Changgeng