/** * @return Currently open transaction. */ private Transaction tx() { return node().transactions().tx(); }
assertNull("Thread already have tx", ignite.transactions().tx());
@Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception { for (TransactionIsolation isolation : TransactionIsolation.values()) { final Transaction tx = ignite.transactions().txStart(OPTIMISTIC, isolation); cache.put(1, 1); cache.put(2, 2); tx.suspend(); assertNull("There is no transaction for current thread", ignite.transactions().tx()); assertEquals(SUSPENDED, tx.state()); GridTestUtils.runAsync(new Runnable() { @Override public void run() { tx.resume(); assertEquals(ACTIVE, tx.state()); cache.put(3, 3); tx.rollback(); } }).get(FUT_TIMEOUT); assertTrue(GridTestUtils.waitForCondition(new PA() { @Override public boolean apply() { return tx.state() == ROLLED_BACK; } }, getTestTimeout())); assertEquals(ROLLED_BACK, tx.state()); assertFalse(cache.containsKey(1)); assertFalse(cache.containsKey(2)); assertFalse(cache.containsKey(3)); cache.removeAll(); } } });
/** * @return Currently open transaction. */ private Transaction tx() { return node().transactions().tx(); }
/** */ private void deactivateWithPendingTransaction(TransactionConcurrency concurrency, TransactionIsolation isolation) { final Ignite ignite0 = grid(0); final IgniteCache<Object, Object> cache0 = ignite0.cache(CACHE_NAME); try (Transaction ignore = ignite0.transactions().txStart(concurrency, isolation)) { cache0.put(1, "1"); //noinspection ThrowableNotThrown GridTestUtils.assertThrowsAnyCause(log, new Callable<Object>() { @Override public Object call() { grid(0).cluster().active(false); return null; } }, IgniteException.class, "Failed to deactivate cluster (must invoke the method outside of an active transaction)."); } assertNull(cache0.get(1)); assertNull(ignite0.transactions().tx()); }
/** * Checks if this operation is available to be used in transaction. * * @throws IgniteException - in case of atomic operation inside transaction without permission. */ private void checkAtomicOpsInTx(CacheOperationContext opCtx) throws IgniteException { if (ctx.atomic() && !opCtx.allowedAtomicOpsInTx()) { if (ctx.grid().transactions().tx() != null) { throw new IgniteException("Transaction spans operations on atomic cache " + "(don't use atomic cache inside transaction or set up flag by cache.allowedAtomicOpsInTx())."); } } } }
/** * Success if explicit tx fails. * * @param cache Cache name. * @param ignite Ignite instance. * @throws Exception If failed. */ protected void checkExplicitTxTimeout(final IgniteCache<Object, Object> cache, final Ignite ignite) throws Exception { try (final Transaction tx = ignite.transactions().txStart()) { assert tx != null; cache.put("key0", "val0"); sleepForTxFailure(); cache.put("key", "val"); fail("Timeout exception must be thrown"); } catch (CacheException e) { assert e.getCause() instanceof TransactionTimeoutException; } assertNull(ignite.transactions().tx()); assert !cache.containsKey("key0"); assert !cache.containsKey("key"); // New transaction must succeed. try (final Transaction tx = ignite.transactions().txStart()) { cache.put("key", "val"); tx.commit(); } assert cache.containsKey("key"); }
/** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { assert jcache().unwrap(Ignite.class).transactions().tx() == null; assertEquals(0, jcache().localSize()); }
assertNull(txs.tx());
/** * @param cache Cache. */ private void testInvokeAll(final IgniteCache<Object, Object> cache) throws Exception { Map<Object, EntryProcessorResult<Object>> results = cache.invokeAll(Collections.singleton("1"), new UpdateProcessor()); final EntryProcessorResult<Object> epRes = F.first(results.values()); assertNotNull(epRes); // In transactions EP will be invoked locally. Class<? extends Exception> exp = grid("client").transactions().tx() == null || ((IgniteCacheProxy)cache).context().mvccEnabled() ? EntryProcessorException.class : NonSerializableException.class; //noinspection ThrowableNotThrown assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { epRes.get(); return null; } }, exp, null); if (ATOMIC_CACHE.equals(cache.getName())) { //noinspection ThrowableNotThrown assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { cache.invokeAll(Collections.singleton("1"), new UpdateValueProcessor()); return null; } }, CachePartialUpdateException.class, null); } }
/** * @param factory Factory. */ private void doTestCommitError(Supplier<Ignite> factory) throws Exception { Ignite crd = startGridsMultiThreaded(nodesCnt); crd.cache(DEFAULT_CACHE_NAME).put(KEY, KEY); Ignite ignite = factory.get(); if (ignite == null) ignite = startGrid("client"); assertNotNull(ignite.cache(DEFAULT_CACHE_NAME)); injectMockedTxManager(ignite); checkKey(); IgniteTransactions transactions = ignite.transactions(); try(Transaction tx = transactions.txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ, 0, 1)) { assertNotNull(transactions.tx()); ignite.cache(DEFAULT_CACHE_NAME).put(KEY, KEY + 1); tx.commit(); fail(); } catch (Exception t) { // No-op. } checkKey(); checkFutures(); }
/** * @param node Node. */ private void checkNoTx(Ignite node) { IgniteTransactions txs = node.transactions(); assertNull(txs.tx()); assertNull(((IgniteKernal)node).context().cache().context().tm().tx()); } }
assertFalse("Must be removed by rollback on timeout", near.cache(CACHE_NAME).containsKey(key + 1)); assertNull(near.transactions().tx());
/** * @param cache Cache. */ private void testInvoke(final IgniteCache<Object, Object> cache) throws Exception { Class<? extends Exception> exp = grid("client").transactions().tx() == null || ((IgniteCacheProxy)cache).context().mvccEnabled() ? EntryProcessorException.class : NonSerializableException.class; //noinspection ThrowableNotThrown assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { cache.invoke("1", new UpdateProcessor()); return null; } }, exp, null); if (ATOMIC_CACHE.equals(cache.getName())) { //noinspection ThrowableNotThrown assertThrows(log, new Callable<Object>() { @Override public Object call() throws Exception { cache.invoke("1", new UpdateValueProcessor()); return null; } }, CachePartialUpdateException.class, null); } }
/** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { super.beforeTest(); if (testsCfg.awaitPartitionMapExchange()) awaitPartitionMapExchange(); assert jcache().unwrap(Ignite.class).transactions().tx() == null; assertEquals(0, jcache().localSize()); assertEquals(0, jcache().size()); }
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { TestIndexingSpi.forceFail(false); Transaction tx = jcache().unwrap(Ignite.class).transactions().tx(); if (tx != null) { tx.close(); fail("Cache transaction remained after test completion: " + tx); } for (int key = 0; key <= lastKey; key++) grid(0).cache(DEFAULT_CACHE_NAME).remove(key); assertEquals(0, jcache(0).size(CachePeekMode.ALL)); }
IgniteCache<Integer, String> cache = jcache(j); Transaction tx = ignite(j).transactions().tx();
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { for (int i = 0; i < GRID_CNT; i++) { near(grid(i)).removeAll(); assertEquals("Near cache size is not zero for grid: " + i, 0, near(grid(i)).size()); assertEquals("DHT cache size is not zero for grid: " + i, 0, dht(grid(i)).size()); assert near(grid(i)).localSize() == 0 : "Near cache is not empty for grid: " + i; assert dht(grid(i)).isEmpty() : "DHT cache is not empty for grid: " + i; } for (int i = 0; i < GRID_CNT; i++) { Transaction tx = grid(i).transactions().tx(); if (tx != null) tx.close(); } }
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { for (int i = 0; i < GRID_CNT; i++) { jcache(i).removeAll(); assertEquals("Near cache size is not zero for grid: " + i, 0, jcache(i).localSize()); assertEquals("DHT cache size is not zero for grid: " + i, 0, dht(grid(i)).size()); assert jcache(i).localSize() == 0 : "Near cache is not empty for grid: " + i; assert dht(grid(i)).isEmpty() : "DHT cache is not empty for grid: " + i; } store.reset(); for (int i = 0; i < GRID_CNT; i++) { Transaction tx = grid(i).transactions().tx(); if (tx != null) { error("Ending zombie transaction: " + tx); tx.close(); } } }