Search in sources :

Example 51 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.

the class WithKeepBinaryCacheFullApiTest method testInvokeTx.

/**
 * @throws Exception If failed.
 */
@Test
public void testInvokeTx() throws Exception {
    if (!txShouldBeUsed())
        return;
    for (TransactionConcurrency conc : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            info(">>>>> Executing test using explicit txs [concurrency=" + conc + ", isolation=" + isolation + "]");
            checkInvokeTx(conc, isolation);
            jcache().removeAll();
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Test(org.junit.Test) IgniteCacheConfigVariationsAbstractTest(org.apache.ignite.testframework.junits.IgniteCacheConfigVariationsAbstractTest)

Example 52 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.

the class IgniteAbstractTxSuspendResumeTest method testResumeTxInAnotherThread.

/**
 * Test for transaction starting in one thread, continuing in another.
 *
 * @throws Exception If failed.
 */
@Test
public void testResumeTxInAnotherThread() throws Exception {
    executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>() {

        @Override
        public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                final Transaction tx = ignite.transactions().txStart(transactionConcurrency(), isolation);
                final AtomicInteger cntr = new AtomicInteger(0);
                for (int j = -1; j > -10; j--) cache.put(j, j);
                cache.put(cntr.get(), cntr.getAndIncrement());
                tx.suspend();
                assertEquals(SUSPENDED, tx.state());
                assertNull("Thread already have tx", ignite.transactions().tx());
                assertNull(cache.get(-1));
                assertNull(cache.get(cntr.get()));
                for (int i = 0; i < 10; i++) {
                    GridTestUtils.runAsync(() -> {
                        assertEquals(SUSPENDED, tx.state());
                        tx.resume();
                        assertEquals(ACTIVE, tx.state());
                        for (int j = -1; j > -10; j--) cache.put(j, j);
                        cache.put(cntr.get(), cntr.getAndIncrement());
                        tx.suspend();
                    }).get(FUT_TIMEOUT);
                }
                tx.resume();
                for (int j = -1; j > -10; j--) cache.remove(j);
                tx.commit();
                assertEquals(COMMITTED, tx.state());
                for (int i = 0; i < cntr.get(); i++) assertEquals(i, (int) cache.get(i));
                assertFalse(cache.containsKey(-1));
                cache.removeAll();
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) IgniteException(org.apache.ignite.IgniteException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 53 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.

the class IgniteAbstractTxSuspendResumeTest method testMultiTxSuspendResume.

/**
 * Test for starting and suspending transactions, and then resuming and committing in another thread.
 *
 * @throws Exception If failed.
 */
@Test
public void testMultiTxSuspendResume() throws Exception {
    executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>() {

        @Override
        public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                final List<Transaction> clientTxs = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    Transaction tx = ignite.transactions().txStart(transactionConcurrency(), isolation);
                    cache.put(i, i);
                    tx.suspend();
                    clientTxs.add(tx);
                }
                GridTestUtils.runMultiThreaded(new CI1<Integer>() {

                    @Override
                    public void apply(Integer idx) {
                        Transaction tx = clientTxs.get(idx);
                        assertEquals(SUSPENDED, tx.state());
                        tx.resume();
                        assertEquals(ACTIVE, tx.state());
                        tx.commit();
                    }
                }, 10, "th-suspend");
                for (int i = 0; i < 10; i++) assertEquals(i, (int) cache.get(i));
                cache.removeAll();
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteException(org.apache.ignite.IgniteException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 54 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.

the class IgniteCacheTxIteratorSelfTest method checkTxCache.

/**
 * @throws Exception If failed.
 */
private void checkTxCache(CacheMode mode, CacheAtomicityMode atomMode, boolean nearEnabled, boolean useEvicPlc) throws Exception {
    if (atomMode == CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) {
        if (!MvccFeatureChecker.isSupported(mode) || (nearEnabled && !MvccFeatureChecker.isSupported(Feature.NEAR_CACHE)) || (useEvicPlc && !MvccFeatureChecker.isSupported(Feature.EVICTION)))
            // Nothing to do. Mode is not supported.
            return;
    }
    final Ignite ignite = grid(0);
    final CacheConfiguration<String, TestClass> ccfg = cacheConfiguration(mode, atomMode, nearEnabled, useEvicPlc);
    final IgniteCache<String, TestClass> cache = ignite.createCache(ccfg).withAllowAtomicOpsInTx();
    info("Checking cache [mode=" + mode + ", atomMode=" + atomMode + ", near=" + nearEnabled + ", evict=" + useEvicPlc + ']');
    try {
        for (int i = 0; i < 30; i++) {
            final TestClass val = new TestClass("data");
            final String key = "key-" + i;
            cache.put(key, val);
            assertEquals(i + 1, cache.size());
            for (TransactionIsolation iso : TransactionIsolation.values()) {
                for (TransactionConcurrency con : TransactionConcurrency.values()) {
                    if (atomMode == CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT && !MvccFeatureChecker.isSupported(con, iso))
                        // Mode not supported.
                        continue;
                    try (Transaction transaction = ignite.transactions().txStart(con, iso)) {
                        assertEquals(val, cache.get(key));
                        transaction.commit();
                    }
                    int cnt = iterateOverKeys(cache);
                    assertEquals("Failed [con=" + con + ", iso=" + iso + ']', i + 1, cnt);
                    assertEquals("Failed [con=" + con + ", iso=" + iso + ']', i + 1, cache.size());
                }
            }
        }
    } finally {
        grid(0).destroyCache(CACHE_NAME);
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite)

Example 55 with TransactionIsolation

use of org.apache.ignite.transactions.TransactionIsolation in project ignite by apache.

the class WrongQueryEntityFieldTypeTest method testPutFromThinClientTx.

/**
 */
@Test
public void testPutFromThinClientTx() throws Exception {
    if (mode == ATOMIC)
        return;
    withThinClient((cli, cache) -> {
        for (TransactionConcurrency conc : TransactionConcurrency.values()) {
            for (TransactionIsolation iso : TransactionIsolation.values()) {
                if (conc == OPTIMISTIC && mode == TRANSACTIONAL_SNAPSHOT)
                    continue;
                assertThrowsWithCause(() -> {
                    try (ClientTransaction tx = cli.transactions().txStart(conc, iso)) {
                        cache.put(1, val.get());
                        tx.commit();
                    }
                }, ClientException.class);
                assertNull(cache.withKeepBinary().get(1));
            }
        }
    });
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ClientTransaction(org.apache.ignite.client.ClientTransaction) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)88 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)71 Test (org.junit.Test)53 Transaction (org.apache.ignite.transactions.Transaction)52 Ignite (org.apache.ignite.Ignite)33 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)27 IgniteCache (org.apache.ignite.IgniteCache)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)12 ArrayList (java.util.ArrayList)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgniteEx (org.apache.ignite.internal.IgniteEx)9 IgniteException (org.apache.ignite.IgniteException)8 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)7 HashMap (java.util.HashMap)6 Map (java.util.Map)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 TransactionTimeoutException (org.apache.ignite.transactions.TransactionTimeoutException)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4