在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,线程=任务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, 标志= 0]]],准备= 0,锁定= true, nodeId = 962ec8e9-c7bd-4b73-b4d3-078da58f4439,locMapped = false, expiryPlc = null,transferExpiryPlc = false,标志= 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,隐式= false,loc = true,threadId = 3770, startTime = 1554721555785,nodeId = 6fb5bb88-fc57-478e-9fb9-c26cc8a311e8, startVer = GridCacheVersion [topVer = 166176849,订单= 1554721151514, nodeOrder = 44],endVer = null,隔离度= REPEATABLE_READ, concurrency = PESSIMISTIC,超时= 0,sysInvalidate = false,sys = false, plc = 2,commitVer = null,finalizing = NONE,invalidParts = null, 状态=活动,超时=假,topVer = AffinityTopologyVersion [topVer = 44,minorTopVer = 0],txCounters = null,持续时间= 156ms, onePhaseCommit = false],尺寸= 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 ,未发生任何错误,但无法实现事务锁定。谁能解释我在哪里弄错了?
答案 0 :(得分:1)
也许您确实未能完成某些交易。
我的建议是在try()
子句中打开交易,并在close()
中finally{}
进行交易