首页 > 解决方案 > java.lang.IllegalStateException:启动新事务失败(当前线程已有事务

问题描述

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

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

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

对于案例 2,我们得到下面提到的错误,

java.lang.IllegalStateException:启动新事务失败(当前线程已有事务):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=1554721151] nodeOrder 44],writeVer=null,隐式=false,loc=true,threadId=3770,startTime=1554721555785,nodeId=6fb5bb88-fc57-478e-9fb9-c26cc8a311e8,startVer=GridCacheVersion [topVer=166176849,order=1554721151514,nodeOrder=44 ],endVer=null,隔离=REPEATABLE_READ,并发=PESSIMISTIC,超时=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]]]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, 隔离=REPEATABLE_READ, concurrency=PESSIMISTIC, timeout=0,sysInvalidate=0,假,sys=false,plc=2,commitVer=null,finalizing=NONE,invalidParts=null,state=ACTIVE,timedOut=false,topVer=AffinityTopologyVersion [topVer=44,minorTopVer=0],txCounters=null,持续时间=156ms , onePhaseCommit=false], 大小=1]]]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, 隔离=REPEATABLE_READ, concurrency=PESSIMISTIC, timeout=0,sysInvalidate=0,假,sys=false,plc=2,commitVer=null,finalizing=NONE,invalidParts=null,state=ACTIVE,timedOut=false,topVer=AffinityTopologyVersion [topVer=44,minorTopVer=0],txCounters=null,持续时间=156ms , onePhaseCommit=false], 大小=1]]]order=1554721151514,nodeOrder=44],writeVer=null,implicit=false,loc=true,threadId=3770,startTime=1554721555785,nodeId=6fb5bb88-fc57-478e-9fb9-c26cc8a311e8,startVer=GridCacheVersion [topVer=166176849,顺序=1554721151514,nodeOrder=44],endVer=null,隔离=REPEATABLE_READ,并发=PESSIMISTIC,超时=0,sysInvalidate=false,sys=false,plc=2,commitVer=null,finalizing=NONE,invalidParts=null,state=活动,timedOut=false,topVer=AffinityTopologyVersion [topVer=44,minorTopVer=0],txCounters=null,duration=156ms,onePhaseCommit=false],size=1]]]order=1554721151514,nodeOrder=44],writeVer=null,implicit=false,loc=true,threadId=3770,startTime=1554721555785,nodeId=6fb5bb88-fc57-478e-9fb9-c26cc8a311e8,startVer=GridCacheVersion [topVer=166176849,顺序=1554721151514,nodeOrder=44],endVer=null,隔离=REPEATABLE_READ,并发=PESSIMISTIC,超时=0,sysInvalidate=false,sys=false,plc=2,commitVer=null,finalizing=NONE,invalidParts=null,state=活动,timedOut=false,topVer=AffinityTopologyVersion [topVer=44,minorTopVer=0],txCounters=null,duration=156ms,onePhaseCommit=false],size=1]]]并发=PESSIMISTIC,超时=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, 持续时间=156ms, onePhaseCommit=false], size=1]]]并发=PESSIMISTIC,超时=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, 持续时间=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,没有发生错误,但无法实现事务锁定。谁能解释我哪里错了?

标签: javamysqlmultithreadingtransactionsignite

解决方案


也许您确实未能完成某些交易。

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


推荐阅读