/** * Commit and properly close transaction. * @param tx Transaction. * @throws IgniteCheckedException if failed. */ private void doCommit(@NotNull GridNearTxLocal tx) throws IgniteCheckedException { try { // TODO: Why checking for rollback only? //if (!tx.isRollbackOnly()) tx.commit(); } finally { closeTx(tx); } }
/** * Execute closure inside cache transaction. * * @param cache Cache. * @param concurrency Concurrency. * @param isolation Isolation. * @param clo Closure. * @throws IgniteCheckedException If failed. */ public static <K, V> void inTx(IgniteInternalCache<K, V> cache, TransactionConcurrency concurrency, TransactionIsolation isolation, IgniteInClosureX<IgniteInternalCache<K ,V>> clo) throws IgniteCheckedException { try (GridNearTxLocal tx = cache.txStartEx(concurrency, isolation)) { clo.applyx(cache); tx.commit(); } }
@Override public CountDownLatch call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheCountDownLatchValue val = cacheView.get(key); if (val == null) { if (log.isDebugEnabled()) log.debug("Failed to find count down latch with given name: " + name); return new CountDownLatch(0); } tx.commit(); return new CountDownLatch(val.get()); } } });
/** * Commits active transaction if exists. * * @throws IgniteCheckedException If failed. */ private void finishActiveTxIfNecessary() throws IgniteCheckedException { try (GridNearTxLocal tx = MvccUtils.tx(ctx)) { if (tx == null) return; if (!tx.isRollbackOnly()) tx.commit(); else tx.rollback(); } }
@Override public Void call() throws Exception { GridCacheAdapter dataCache = dataCache(attacker); try (GridNearTxLocal tx = dataCache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) { dataCache.put(DATA_KEY, 0); txStartLatch.countDown(); txCommitLatch.await(); tx.commit(); } return null; } });
@Override public Boolean call() throws Exception { boolean retVal; try (GridNearTxLocal tx = cache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) { Long idx = (Long)cache.invoke(queueKey, new AddProcessor(id, items.size())).get(); if (idx != null) { checkRemoved(idx); Map<QueueItemKey, T> putMap = new HashMap<>(); for (T item : items) { putMap.put(itemKey(idx), item); idx++; } cache.putAll(putMap); retVal = true; } else retVal = false; tx.commit(); return retVal; } } });
@Override public T call() throws Exception { T retVal; while (true) { try (GridNearTxLocal tx = cache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) { Long idx = (Long)cache.invoke(queueKey, new PollProcessor(id)).get(); if (idx != null) { checkRemoved(idx); retVal = (T)cache.getAndRemove(itemKey(idx)); if (retVal == null) { // Possible if data was lost. tx.commit(); continue; } } else retVal = null; tx.commit(); return retVal; } } } });
@Override public Void call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheAtomicReferenceValue<T> ref = cacheView.get(key); if (ref == null) throw new IgniteException("Failed to find atomic reference with given name: " + name); cacheView.put(key, new GridCacheAtomicReferenceValue<>(val)); tx.commit(); } return null; } });
/** {@inheritDoc} */ @Override public final T apply(T t, @Nullable Exception e) { boolean rollback = true; try { if (e != null) throw new GridClosureException(e); t = finish(t); // Commit implicit transactions. if (implicit()) commit(); rollback = false; return t; } catch (IgniteCheckedException ex) { throw new GridClosureException(ex); } finally { if (rollback) setRollbackOnly(); } }
@Override public Void call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheAtomicStampedValue<T, S> ref = cacheView.get(key); if (ref == null) throw new IgniteException("Failed to find atomic stamped with given name: " + name); cacheView.put(key, new GridCacheAtomicStampedValue<>(val, stamp)); tx.commit(); } return null; } });
@Override public Object call() throws Exception { try (GridNearTxLocal tx = cache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) { Long idx = (Long)cache.invoke(queueKey, new RemoveProcessor(id, rmvIdx)).get(); if (idx != null) { checkRemoved(idx); cache.remove(itemKey(idx)); } tx.commit(); } return null; } });
/** * Set sampling flag. * * @param val Sampling flag state or {@code null} to clear sampling state and mark it as "not set". * @return {@code True} if sampling mode was actually changed by this call. * @throws IgniteCheckedException If failed. */ public boolean sampling(Boolean val) throws IgniteCheckedException { if (busyLock.enterBusy()) { try { validTxState(false); try (GridNearTxLocal tx = startTx()) { Object prev = val != null ? metaCache.getAndPut(sampling, val) : metaCache.getAndRemove(sampling); tx.commit(); return !F.eq(prev, val); } } finally { busyLock.leaveBusy(); } } else throw new IllegalStateException("Failed to set sampling flag because Grid is stopping."); }
@Override public Boolean call() throws Exception { boolean retVal; try (GridNearTxLocal tx = cache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) { Long idx = (Long)cache.invoke(queueKey, new AddProcessor(id, 1)).get(); if (idx != null) { checkRemoved(idx); cache.getAndPut(itemKey(idx), item); retVal = true; } else retVal = false; tx.commit(); return retVal; } } });
@Override public Boolean call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheAtomicReferenceValue<T> ref = cacheView.get(key); if (ref == null) throw new IgniteException("Failed to find atomic reference with given name: " + name); T curVal = ref.get(); if (!F.eq(expVal, curVal)) return false; else { cacheView.put(key, new GridCacheAtomicReferenceValue<>(newVal)); tx.commit(); return true; } } } });
@Override public T call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheAtomicReferenceValue<T> ref = cacheView.get(key); if (ref == null) throw new IgniteException("Failed to find atomic reference with given name: " + name); T curVal = ref.get(); if (!F.eq(expVal, curVal)) return curVal; else { cacheView.put(key, new GridCacheAtomicReferenceValue<>(newVal)); tx.commit(); return expVal; } } } });
@Override public Boolean call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheAtomicStampedValue<T, S> val = cacheView.get(key); if (val == null) throw new IgniteException("Failed to find atomic stamped with given name: " + name); if (F.eq(expVal, val.value()) && F.eq(expStamp, val.stamp())) { cacheView.put(key, new GridCacheAtomicStampedValue<>(newVal, newStamp)); tx.commit(); return true; } return false; } } });
/** {@inheritDoc} */ @Override public Integer call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheCountDownLatchValue latchVal = cacheView.get(key); if (latchVal == null) { if (log.isDebugEnabled()) log.debug("Failed to find count down latch with given name: " + name); return 0; } int retVal; if (val > 0) { retVal = latchVal.get() - val; if (retVal < 0) retVal = 0; } else retVal = 0; latchVal.set(retVal); cacheView.put(key, latchVal); tx.commit(); return retVal; } } }
@Override public Sync call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheSemaphoreState val = cacheView.get(key); if (val == null) { if (log.isDebugEnabled()) log.debug("Failed to find semaphore with given name: " + name); return null; } final int cnt = val.getCount(); Map<UUID, Integer> waiters = val.getWaiters(); final boolean failoverSafe = val.isFailoverSafe(); tx.commit(); Sync sync = new Sync(cnt, waiters, failoverSafe); sync.setBroken(val.isBroken()); return sync; } } });
/** * Success if explicit tx doesn't fail. * * @param cache Cache instance. * @throws Exception If failed. */ protected void checkStartTxSuccess(final IgniteInternalCache<Object, Object> cache) throws Exception { try (final GridNearTxLocal tx = CU.txStartInternal(cache.context(), cache, PESSIMISTIC, REPEATABLE_READ)) { assert tx != null; sleepForTxFailure(); cache.put("key", "val"); tx.commit(); } assert cache.containsKey("key"); cache.clear(); }
/** * @throws Exception If failed. */ @Test public void testSystemTxInsideUserTx() throws Exception { Assume.assumeFalse("https://issues.apache.org/jira/browse/IGNITE-10473", MvccFeatureChecker.forcedMvcc()); IgniteKernal ignite = (IgniteKernal)grid(0); IgniteCache<Object, Object> jcache = ignite.cache(DEFAULT_CACHE_NAME); try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { jcache.get("1"); jcache.put("1", "11"); IgniteInternalCache<Object, Object> utilityCache = ignite.context().cache().utilityCache(); utilityCache.getAndPutIfAbsent("2", "2"); try (GridNearTxLocal itx = utilityCache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) { assertEquals(null, utilityCache.get("1")); assertEquals("2", utilityCache.get("2")); assertEquals(null, utilityCache.get("3")); utilityCache.getAndPut("3", "3"); itx.commit(); } jcache.put("2", "22"); tx.commit(); } checkTransactionsCommitted(); checkEntries(DEFAULT_CACHE_NAME, "1", "11", "2", "22", "3", null); checkEntries(CU.UTILITY_CACHE_NAME, "1", null, "2", "2", "3", "3"); }