Search in sources :

Example 56 with TransactionIsolation

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

the class GridJtaTransactionManagerSelfTest method testJtaTxContextSwitch.

/**
 * Test for switching tx context by JTA Manager.
 *
 * @throws Exception If failed.
 */
@Test
public void testJtaTxContextSwitch() throws Exception {
    for (TransactionIsolation isolation : TransactionIsolation.values()) {
        TransactionConfiguration cfg = grid().context().config().getTransactionConfiguration();
        cfg.setDefaultTxConcurrency(txConcurrency);
        cfg.setDefaultTxIsolation(isolation);
        TransactionManager jtaTm = jotm.getTransactionManager();
        IgniteCache<Integer, String> cache = jcache();
        assertNull(grid().transactions().tx());
        jtaTm.begin();
        Transaction tx1 = jtaTm.getTransaction();
        cache.put(1, Integer.toString(1));
        assertNotNull(grid().transactions().tx());
        assertEquals(ACTIVE, grid().transactions().tx().state());
        assertEquals(Integer.toString(1), cache.get(1));
        jtaTm.suspend();
        assertNull(grid().transactions().tx());
        assertNull(cache.get(1));
        jtaTm.begin();
        Transaction tx2 = jtaTm.getTransaction();
        assertNotSame(tx1, tx2);
        cache.put(2, Integer.toString(2));
        assertNotNull(grid().transactions().tx());
        assertEquals(ACTIVE, grid().transactions().tx().state());
        assertEquals(Integer.toString(2), cache.get(2));
        jtaTm.commit();
        assertNull(grid().transactions().tx());
        assertEquals(Integer.toString(2), cache.get(2));
        jtaTm.resume(tx1);
        assertNotNull(grid().transactions().tx());
        assertEquals(ACTIVE, grid().transactions().tx().state());
        cache.put(3, Integer.toString(3));
        jtaTm.commit();
        assertEquals("1", cache.get(1));
        assertEquals("2", cache.get(2));
        assertEquals("3", cache.get(3));
        assertNull(grid().transactions().tx());
        cache.removeAll();
    }
}
Also used : TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 57 with TransactionIsolation

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

the class GridJtaTransactionManagerSelfTest method testJtaTxContextSwitchWithExistingTx.

/**
 * @throws Exception If failed.
 */
@Test
public void testJtaTxContextSwitchWithExistingTx() throws Exception {
    for (TransactionIsolation isolation : TransactionIsolation.values()) {
        TransactionConfiguration cfg = grid().context().config().getTransactionConfiguration();
        cfg.setDefaultTxConcurrency(txConcurrency);
        cfg.setDefaultTxIsolation(isolation);
        TransactionManager jtaTm = jotm.getTransactionManager();
        IgniteCache<Integer, String> cache = jcache();
        jtaTm.begin();
        Transaction tx1 = jtaTm.getTransaction();
        cache.put(1, Integer.toString(1));
        assertNotNull(grid().transactions().tx());
        assertEquals(ACTIVE, grid().transactions().tx().state());
        assertEquals(Integer.toString(1), cache.get(1));
        jtaTm.suspend();
        jtaTm.begin();
        Transaction tx2 = jtaTm.getTransaction();
        assertNotSame(tx1, tx2);
        cache.put(2, Integer.toString(2));
        try {
            jtaTm.resume(tx1);
            fail("jtaTm.resume shouldn't success.");
        } catch (IllegalStateException ignored) {
        // No-op.
        } finally {
            // rolling back tx2
            jtaTm.rollback();
        }
        jtaTm.resume(tx1);
        jtaTm.rollback();
        cache.removeAll();
    }
}
Also used : TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 58 with TransactionIsolation

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

the class IgniteCacheInvokeReadThroughAbstractTest method invokeReadThrough.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
protected void invokeReadThrough(CacheConfiguration ccfg) throws Exception {
    Ignite ignite0 = ignite(0);
    ignite0.createCache(ccfg);
    int key = 0;
    for (Ignite node : G.allGrids()) {
        if (node.configuration().isClientMode() && ccfg.getNearConfiguration() != null)
            node.createNearCache(ccfg.getName(), ccfg.getNearConfiguration());
    }
    for (Ignite node : G.allGrids()) {
        log.info("Test for node: " + node.name());
        IgniteCache<Object, Object> cache = node.cache(ccfg.getName());
        for (int i = 0; i < 50; i++) checkReadThrough(cache, key++, null, null);
        Set<Object> keys = new HashSet<>();
        for (int i = 0; i < 5; i++) keys.add(key++);
        checkReadThroughInvokeAll(cache, keys, null, null);
        keys = new HashSet<>();
        for (int i = 0; i < 100; i++) keys.add(key++);
        checkReadThroughInvokeAll(cache, keys, null, null);
        if (ccfg.getAtomicityMode() == TRANSACTIONAL) {
            for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                for (TransactionIsolation isolation : TransactionIsolation.values()) {
                    log.info("Test tx [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    for (int i = 0; i < 50; i++) checkReadThrough(cache, key++, concurrency, isolation);
                    keys = new HashSet<>();
                    for (int i = 0; i < 5; i++) keys.add(key++);
                    checkReadThroughInvokeAll(cache, keys, concurrency, isolation);
                    keys = new HashSet<>();
                    for (int i = 0; i < 100; i++) keys.add(key++);
                    checkReadThroughInvokeAll(cache, keys, concurrency, isolation);
                }
            }
            for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                for (TransactionIsolation isolation : TransactionIsolation.values()) {
                    log.info("Test tx2 [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                    for (int i = 0; i < 50; i++) checkReadThroughGetAndInvoke(cache, key++, concurrency, isolation);
                }
            }
        }
    }
    ignite0.cache(ccfg.getName()).removeAll();
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet)

Example 59 with TransactionIsolation

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

the class IgniteCacheReadThroughStoreCallTest method checkLoadCount.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void checkLoadCount(CacheConfiguration<Object, Object> ccfg) throws Exception {
    storeMap.clear();
    Ignite ignite0 = ignite(0);
    ignite0.createCache(ccfg);
    try {
        int key = 0;
        for (Ignite node : G.allGrids()) {
            log.info("Test for node: " + node.name());
            final IgniteCache<Object, Object> cache = node.cache(ccfg.getName());
            for (int i = 0; i < 50; i++) {
                final int k = key++;
                checkReadThrough(cache, new IgniteRunnable() {

                    @Override
                    public void run() {
                        cache.invoke(k, new TestEntryProcessor());
                    }
                }, null, null, 1);
            }
            for (int i = 0; i < 50; i++) {
                final int k = key++;
                checkReadThrough(cache, new IgniteRunnable() {

                    @Override
                    public void run() {
                        cache.put(k, k);
                    }
                }, null, null, 0);
            }
            if (ccfg.getAtomicityMode() == TRANSACTIONAL) {
                for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                    for (TransactionIsolation isolation : values()) {
                        log.info("Test tx [concurrency=" + concurrency + ", isolation=" + isolation + ']');
                        for (int i = 0; i < 50; i++) {
                            final int k = key++;
                            checkReadThrough(cache, new IgniteRunnable() {

                                @Override
                                public void run() {
                                    cache.invoke(k, new TestEntryProcessor());
                                }
                            }, concurrency, isolation, 2);
                        }
                    }
                }
            }
        }
        ignite0.cache(ccfg.getName()).removeAll();
    } finally {
        ignite0.destroyCache(ccfg.getName());
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 60 with TransactionIsolation

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

the class TxOnCachesStopTest method runTxOnCacheStop.

/**
 * @param block {@code True} To block GridNearTxPrepareRequest message.
 */
private void runTxOnCacheStop(boolean block) throws Exception {
    startGridsMultiThreaded(2);
    IgniteEx ig = startClientGrid("client");
    ig.cluster().active(true);
    for (TransactionConcurrency conc : TransactionConcurrency.values()) {
        for (TransactionIsolation iso : TransactionIsolation.values()) runTxOnCacheStop(conc, iso, ig, block);
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation)

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