/** {@inheritDoc} */ @Override public void clear() { super.clear(); transactionActive = false; transaction.close(); } }
@Override public void applyx(Transaction tx) throws Exception { tx.close(); } },
/** * @param id Transaction ID. * @return Transaction state. */ private int txClose(long id) { Transaction tx = tx(id); try { tx.close(); return tx.state().ordinal(); } finally { unregisterTx(id); } }
@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); try { tx.resume(); fail("Exception must be thrown"); } catch (Throwable e) { assertTrue(X.hasCause(e, IgniteException.class)); assertFalse(X.hasCause(e, AssertionError.class)); } tx.close(); assertFalse(cache.containsKey(1)); } } });
@Override public void applyx(Transaction tx) throws IgniteCheckedException { try { IgniteFuture<Void> rollbackFut = tx.rollbackAsync(); rollbackFut.listen(fut -> tx.close()); } catch (Throwable t) { log.error("Exception on async rollback", t); throw new IgniteCheckedException("Rollback failed", t); } } };
@Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception { for (CI1Exc<Transaction> txOperation : SUSPENDED_TX_PROHIBITED_OPS) { for (TransactionIsolation isolation : TransactionIsolation.values()) { Transaction tx = ignite.transactions().txStart(OPTIMISTIC, isolation); cache.put(1, 1); tx.suspend(); GridTestUtils.assertThrowsWithCause(txOperation, tx, IgniteException.class); tx.resume(); tx.close(); assertNull(cache.get(1)); } } } });
@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, TX_TIMEOUT, 0); cache.put(1, 1); U.sleep(TX_TIMEOUT * 2); GridTestUtils.assertThrowsWithCause(new Callable<Object>() { @Override public Object call() throws Exception { tx.suspend(); return null; } }, TransactionTimeoutException.class); assertTrue(GridTestUtils.waitForCondition(new PA() { @Override public boolean apply() { return tx.state() == ROLLED_BACK; } }, getTestTimeout())); assertEquals(ROLLED_BACK, tx.state()); tx.close(); assertNull(cache.get(1)); } } });
@Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception { for (final CI1Exc<Transaction> txOperation : SUSPENDED_TX_PROHIBITED_OPS) { for (TransactionIsolation isolation : TransactionIsolation.values()) { final Transaction tx = ignite.transactions().txStart(OPTIMISTIC, isolation); cache.put(1, 1); tx.suspend(); multithreaded(new RunnableX() { @Override public void runx() throws Exception { GridTestUtils.assertThrowsWithCause(txOperation, tx, IgniteException.class); } }, 1); tx.resume(); tx.close(); assertNull(cache.get(1)); } } } });
/** * @param key Key. * @param txMode Non null tx mode if explicit transaction should be started. * @throws Exception If failed. */ private void checkVersion(String key, @Nullable TransactionConcurrency txMode) throws Exception { IgniteCache<String, Integer> cache = jcache(0); Transaction tx = null; if (txMode != null) tx = cache.unwrap(Ignite.class).transactions().txStart(txMode, REPEATABLE_READ); try { cache.put(key, 1); if (tx != null) tx.commit(); } finally { if (tx != null) tx.close(); } checkEntryVersion(key); }
@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, TX_TIMEOUT, 0); cache.put(1, 1); tx.suspend(); U.sleep(TX_TIMEOUT * 2); GridTestUtils.assertThrowsWithCause(new Callable<Object>() { @Override public Object call() throws Exception { tx.resume(); return null; } }, TransactionTimeoutException.class); assertTrue(GridTestUtils.waitForCondition(new PA() { @Override public boolean apply() { return tx.state() == ROLLED_BACK; } }, getTestTimeout())); assertEquals(ROLLED_BACK, tx.state()); tx.close(); } } });
/** * @param cache Cache. * @param key Key. * @param concurrency Transaction concurrency. * @param isolation Transaction isolation. * @throws Exception If failed. */ private void checkReadThrough(IgniteCache<Object, Object> cache, Object key, @Nullable TransactionConcurrency concurrency, @Nullable TransactionIsolation isolation) throws Exception { putDataInStore(Collections.singletonMap(key, key), cache.getName()); Transaction tx = isolation != null ? cache.unwrap(Ignite.class).transactions().txStart(concurrency, isolation) : null; try { Object ret = cache.invoke(key, new TestEntryProcessor()); assertEquals(key, ret); if (tx != null) tx.commit(); } finally { if (tx != null) tx.close(); } checkValue(cache.getName(), key, (Integer)key + 1); }
@Override public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception { for (TransactionIsolation tx1Isolation : TransactionIsolation.values()) { for (TransactionIsolation tx2Isolation : TransactionIsolation.values()) { Transaction tx1 = ignite.transactions().txStart(OPTIMISTIC, tx1Isolation); cache.put(1, 1); tx1.suspend(); assertFalse(cache.containsKey(1)); Transaction tx2 = ignite.transactions().txStart(OPTIMISTIC, tx2Isolation); cache.put(1, 2); tx2.commit(); assertEquals(2, (int)cache.get(1)); tx1.resume(); assertEquals(1, (int)cache.get(1)); tx1.close(); cache.removeAll(); } } } });
/** * Tests sequential value write and read inside transaction. * @param concurrency Transaction concurrency. * @param isolation Transaction isolation. * @throws IgniteCheckedException If failed */ protected void checkTransactionalRead(TransactionConcurrency concurrency, TransactionIsolation isolation) throws IgniteCheckedException { IgniteCache<String, Integer> cache = jcache(0); cache.clear(); Transaction tx = grid(0).transactions().txStart(concurrency, isolation); try { cache.put("key", 1); assertEquals("Invalid value after put", 1, cache.get("key").intValue()); tx.commit(); } finally { tx.close(); } assertEquals("Invalid cache size after put", 1, cache.size()); try { tx = grid(0).transactions().txStart(concurrency, isolation); assertEquals("Invalid value inside transactional read", Integer.valueOf(1), cache.get("key")); tx.commit(); } finally { tx.close(); } }
/** {@inheritDoc} */ @Override protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { if (definition.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED) throw new InvalidIsolationLevelException("Ignite does not support READ_UNCOMMITTED isolation level."); IgniteTransactionObject txObj = (IgniteTransactionObject)transaction; Transaction tx = null; try { if (txObj.getTransactionHolder() == null || txObj.getTransactionHolder().isSynchronizedWithTransaction()) { long timeout = ignite.configuration().getTransactionConfiguration().getDefaultTxTimeout(); if (definition.getTimeout() > 0) timeout = TimeUnit.SECONDS.toMillis(definition.getTimeout()); Transaction newTx = ignite.transactions().txStart(transactionConcurrency, convertToIgniteIsolationLevel(definition.getIsolationLevel()), timeout, 0); if (log.isDebugEnabled()) log.debug("Started Ignite transaction: " + newTx); txObj.setTransactionHolder(new IgniteTransactionHolder(newTx), true); } txObj.getTransactionHolder().setSynchronizedWithTransaction(true); txObj.getTransactionHolder().setTransactionActive(true); tx = txObj.getTransactionHolder().getTransaction(); // Bind the session holder to the thread. if (txObj.isNewTransactionHolder()) TransactionSynchronizationManager.bindResource(this.ignite, txObj.getTransactionHolder()); } catch (Exception ex) { if (tx != null) tx.close(); throw new CannotCreateTransactionException("Could not create Ignite transaction", ex); } }
/** {@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)); }
/** * @param concurrency Concurrency. * @throws Exception If failed. */ private void checkTransformAfterRemove(TransactionConcurrency concurrency) throws Exception { IgniteCache<String, Integer> cache = jcache(); cache.put("key", 4); Transaction tx = txShouldBeUsed() ? ignite(0).transactions().txStart(concurrency, READ_COMMITTED) : null; try { cache.remove("key"); cache.invoke("key", INCR_PROCESSOR); cache.invoke("key", INCR_PROCESSOR); cache.invoke("key", INCR_PROCESSOR); if (tx != null) tx.commit(); } finally { if (tx != null) tx.close(); } assertEquals((Integer)3, cache.get("key")); }
/** * @param concurrency Concurrency. * @throws Exception If failed. */ private void checkInvokeAfterRemove(TransactionConcurrency concurrency) throws Exception { IgniteCache<Object, Object> cache = jcache(); Object key = key(1); cache.put(key, value(4)); Transaction tx = txShouldBeUsed() ? ignite(0).transactions().txStart(concurrency, READ_COMMITTED) : null; try { cache.remove(key); cache.invoke(key, INCR_PROCESSOR, dataMode); cache.invoke(key, INCR_PROCESSOR, dataMode); cache.invoke(key, INCR_PROCESSOR, dataMode); if (tx != null) tx.commit(); } finally { if (tx != null) tx.close(); } assertEquals(value(3), cache.get(key)); }
/** * @param inTx Whether to start transaction. * @throws Exception If failed. */ private void checkPut(boolean inTx) throws Exception { Transaction tx = inTx ? transactions().txStart() : null; IgniteCache<String, Integer> cache = jcache(); try { cache.put("key1", 1); cache.put("key2", 2); // Check inside transaction. assert cache.get("key1") == 1; assert cache.get("key2") == 2; if (tx != null) tx.commit(); } finally { if (tx != null) tx.close(); } checkSize(F.asSet("key1", "key2")); // Check outside transaction. checkContainsKey(true, "key1"); checkContainsKey(true, "key2"); checkContainsKey(false, "wrong"); assert cache.get("key1") == 1; assert cache.get("key2") == 2; assert cache.get("wrong") == null; }
/** {@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(); } } }