Explicit locking in DIST_SYNC mode
infinikli Nov 12, 2010 12:00 PMHello everybody.
We're currently evaluating infinispan as a data grid. Because we need to have control over the access of the shared objects, we need locking-mechanics. The idea is to lock data-access in the following manner:
public <K,V> boolean performLockedCacheEntryAction(ItemAction<V> action, K key) throws NotSupportedException, SystemException {
TransactionManager tm = null;
try {
Cache<K, V> c = cacheManager.getCache();
/*
* Obtain TransactionManager.
*/
tm = c.getAdvancedCache().getTransactionManager();
/*
* Begin transaction.
*/
tm.begin();
/*
* Lock key clusterwide to avoid concurrent access on the action.
*/
c.getAdvancedCache().lock(key);
/*
* Perform action.
*/
boolean changedState = action.doAction();
if(changedState){
/*
* Put object into cache when state has changed.
*/
c.put(key, action.getItem());
}
/*
* Commit transaction and release locks.
*/
tm.commit();
return changedState;
} catch (Throwable t) {
logger.error(t,t);
if(tm!=null && tm.getStatus() == Status.STATUS_ACTIVE){
logger.debug("Rolling back transaction "+tm);
tm.rollback();
}
return false;
}
}
However when we test this code-snippet on two nodes, the action is sometimes performed twice. The Cache is configured as follows:
Configuration config = new Configuration();
config.setCacheMode(CacheMode.DIST_SYNC);
config.setL1CacheEnabled(true);
config.setL1Lifespan(60000); config.setTransactionManagerLookupClass("org.infinispan.transaction.lookup.JBossStandaloneJTAManagerLookup");
config.setEagerLockSingleNode(true);
cacheManager = new DefaultCacheManager(GlobalConfiguration.getClusteredDefault());
Are we missing something?
N.B: We are using 4.2.0.BETA1 artifacts. All versions below had massive problems with locking which lead into TimeoutExceptions while acquiring the lock.