Updating INFISPAN cache object in transaction return problem

we hope to use infinispan as a memory database in the order management system. There we need to perform the following type of operation. Here, the cache account cache contains the client cache account downloaded from the database. Suppose that the initial balance of cash account1 is 1000 and cashAccount2 is 2000. We update both cash accounts in a transaction on the jboss 7.1 application server. as a result, we expect the balances of both funds to remain unchanged since this transaction occurred within the transaction. But, unfortunately, even after the transaction is rolled back, we can see the update object in the cache. Utah is what we consider when we add an object to the cache on the transaction side, when the transaction rollback is removed from the cache. But the modification of the existing object remains as it is.

This is just an example of what we want to do. Actual - inclusion of several objects in one transaction.

Could you tell us that you can use infinispan for this type of operation.

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) { } } } 
+6
source share
1 answer

The right way to do this in infinispan is to make the CacheAccount object immutable. Otherwise, you change the property of the object, and Infinispan does not control 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) { } } } 
+1
source

Source: https://habr.com/ru/post/956499/


All Articles