Search code examples
javamysqlmultithreadingtransactionsignite

java.lang.IllegalStateException: Failed to start new transaction (current thread already has a transaction


In a transactional approach in Ignite caches, we use a multi-threaded approach.

case 1: multiple threads insert in a cache (No locking) on the same key.

case 2: multiple threads read data from cache (In Transactional locking) with a similar key.

For case 2, we are getting below mentioned error,

java.lang.IllegalStateException: Failed to start new transaction (current thread already has a transaction): GridNearTxLocal [mappings=IgniteTxMappingsImpl [], nearLocallyMapped=false, colocatedLocallyMapped=false, needCheckBackup=null, hasRemoteLocks=true, trackTimeout=false, lb=null, mvccTracker=null, mvccOp=null, thread=Executor task launch worker for task 290, mappings=IgniteTxMappingsImpl [], super=GridDhtTxLocalAdapter [nearOnOriginatingNode=false, nearNodes=[], dhtNodes=[], explicitLock=false, super=IgniteTxLocalAdapter [completedBase=null, sndTransformedVals=false, depEnabled=false, txState=IgniteTxStateImpl [activeCacheIds=[105038815], recovery=false, mvccEnabled=false, txMap=[IgniteTxEntry [key=KeyCacheObjectImpl [part=344, val=abc, hasValBytes=false], cacheId=105038815, txKey=IgniteTxKey [key=KeyCacheObjectImpl [part=344, val=abc, hasValBytes=false], cacheId=105038815], val=[op=READ, val=null], prevVal=[op=NOOP, val=null], oldVal=[op=NOOP, val=null], entryProcessorsCol=null, ttl=-1, conflictExpireTime=-1, conflictVer=null, explicitVer=null, dhtVer=null, filters=null, filtersPassed=false, filtersSet=true, entry=GridDhtDetachedCacheEntry [super=GridDistributedCacheEntry [super=GridCacheMapEntry [key=KeyCacheObjectImpl [part=344, val=abc, hasValBytes=false], val=null, ver=GridCacheVersion [topVer=166176849, order=1554721151514, nodeOrder=1], hash=-1768407104, extras=null, flags=0]]], prepared=0, locked=true, nodeId=962ec8e9-c7bd-4b73-b4d3-078da58f4439, locMapped=false, expiryPlc=null, transferExpiryPlc=false, flags=0, partUpdateCntr=0, serReadVer=null, xidVer=GridCacheVersion [topVer=166176849, order=1554721151514, nodeOrder=44]]]], mvccWaitTxs=null, qryEnlisted=false, forceSkipCompletedVers=false, super=IgniteTxAdapter [xidVer=GridCacheVersion [topVer=166176849, order=1554721151514, nodeOrder=44], writeVer=null, implicit=false, loc=true, threadId=3770, startTime=1554721555785, nodeId=6fb5bb88-fc57-478e-9fb9-c26cc8a311e8, startVer=GridCacheVersion [topVer=166176849, order=1554721151514, nodeOrder=44], endVer=null, isolation=REPEATABLE_READ, concurrency=PESSIMISTIC, timeout=0, sysInvalidate=false, sys=false, plc=2, commitVer=null, finalizing=NONE, invalidParts=null, state=ACTIVE, timedOut=false, topVer=AffinityTopologyVersion [topVer=44, minorTopVer=0], txCounters=null, duration=156ms, onePhaseCommit=false], size=1]]]

Code Snippet as below,

IgniteCache<String, String> cache =  ignite.getOrCreateCache("ABC_CACHE");
Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ);

String acknowledge = cache.get(key);
if(acknowledge == null || acknowledge.length() == 0) {
if(acknowledge.contains("xyz")) {
    acknowledge.append("mln");
}
    flag = true;
    cache.removeAsync(key);
}else {
    cache.putAsync(key, acknowledge);
}
tx.commit();
tx.close();

Also, tried with Isolation level READ_COMMITTED, no error occurred but, could not achieve transaction locking. Can anyone explain where am I getting wrong?


Solution

  • Maybe indeed you failed to close some transaction.

    My recommendation is to open transaction in try() clause, and close() it in finally{}