Search in sources :

Example 86 with TransactionIsolation

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

the class IgniteTxStoreExceptionAbstractSelfTest method testPutPrimaryTx.

/**
 * @throws Exception If failed.
 */
@Test
public void testPutPrimaryTx() throws Exception {
    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            checkPutTx(true, concurrency, isolation, keyForNode(grid(0).localNode(), PRIMARY));
            checkPutTx(false, concurrency, isolation, keyForNode(grid(0).localNode(), PRIMARY));
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Test(org.junit.Test)

Example 87 with TransactionIsolation

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

the class IgniteOnePhaseCommitNearSelfTest method checkKey.

/**
 * @param transactions Transactions instance.
 * @param cache Cache instance.
 * @param key Key.
 */
private void checkKey(IgniteTransactions transactions, Cache<Object, Object> cache, int key) throws Exception {
    cache.put(key, key);
    finalCheck(key, true);
    TransactionIsolation[] isolations = { READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE };
    TransactionConcurrency[] concurrencies = { OPTIMISTIC, PESSIMISTIC };
    for (TransactionIsolation isolation : isolations) {
        for (TransactionConcurrency concurrency : concurrencies) {
            info("Checking transaction [isolation=" + isolation + ", concurrency=" + concurrency + ']');
            try (Transaction tx = transactions.txStart(concurrency, isolation)) {
                cache.put(key, isolation + "-" + concurrency);
                tx.commit();
            }
            finalCheck(key, true);
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

Example 88 with TransactionIsolation

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

the class CacheMvccTransactionsTest method testTxReadIsolationSimple.

/**
 * @throws Exception If failed.
 */
@Test
public void testTxReadIsolationSimple() throws Exception {
    Ignite srv0 = startGrids(4);
    client = true;
    startGrid(4);
    for (CacheConfiguration ccfg : cacheConfigurations()) {
        IgniteCache<Object, Object> cache0 = srv0.createCache(ccfg);
        final Map<Integer, Integer> startVals = new HashMap<>();
        final int KEYS = 10;
        for (int i = 0; i < KEYS; i++) startVals.put(i, 0);
        for (final TransactionIsolation isolation : TransactionIsolation.values()) {
            for (final Ignite node : G.allGrids()) {
                info("Run test [node=" + node.name() + ", isolation=" + isolation + ']');
                try (Transaction tx = srv0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache0.putAll(startVals);
                    tx.commit();
                }
                final CountDownLatch readStart = new CountDownLatch(1);
                final CountDownLatch readProceed = new CountDownLatch(1);
                IgniteInternalFuture fut = GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        IgniteCache<Object, Object> cache = node.cache(DEFAULT_CACHE_NAME);
                        try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            assertEquals(0, checkAndGet(false, cache, 0, SCAN, GET));
                            readStart.countDown();
                            assertTrue(readProceed.await(5, TimeUnit.SECONDS));
                            assertEquals(0, checkAndGet(true, cache, 1, GET, SCAN));
                            assertEquals(0, checkAndGet(true, cache, 2, GET, SCAN));
                            Map<Object, Object> res = checkAndGetAll(true, cache, startVals.keySet(), GET, SCAN);
                            assertEquals(startVals.size(), res.size());
                            for (Map.Entry<Object, Object> e : res.entrySet()) assertEquals("Invalid value for key: " + e.getKey(), 0, e.getValue());
                            tx.rollback();
                        }
                        return null;
                    }
                });
                assertTrue(readStart.await(5, TimeUnit.SECONDS));
                for (int i = 0; i < KEYS; i++) {
                    try (Transaction tx = srv0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        if (i % 2 == 0)
                            cache0.put(i, 1);
                        else
                            cache0.remove(i);
                        tx.commit();
                    }
                }
                readProceed.countDown();
                fut.get();
            }
        }
        srv0.destroyCache(cache0.getName());
    }
}
Also used : HashMap(java.util.HashMap) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableEntry(javax.cache.processor.MutableEntry) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) 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