java.lang.IllegalStateException: 启动新事务失败(当前线程已有事务

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

在 Ignite 缓存的事务性方法中,我们使用多线程方法。

情况 1:多个线程在同一个键上插入缓存(无锁定)。

情况 2:多个线程使用相似的键从缓存中读取数据(在事务锁定中)。

对于情况 2,我们遇到下面提到的错误,

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]]]

代码片段如下,

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();

此外,尝试使用隔离级别READ_COMMITTED,没有发生错误,但无法实现事务锁定。谁能解释我哪里出错了?

也许您确实未能关闭某些交易。

我的建议是在 try() 子句中打开交易,close()finally{}

中打开交易