Search in sources :

Example 1 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IndexingSpiQueryTxSelfTest method testIndexingSpiWithTx.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void testIndexingSpiWithTx() throws Exception {
    IgniteEx ignite = grid(0);
    final IgniteCache<Integer, Integer> cache = ignite.cache("test-cache");
    final IgniteTransactions txs = ignite.transactions();
    for (final TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (final TransactionIsolation isolation : TransactionIsolation.values()) {
            System.out.println("Run in transaction: " + concurrency + " " + isolation);
            GridTestUtils.assertThrowsWithCause(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Transaction tx;
                    try (Transaction tx0 = tx = txs.txStart(concurrency, isolation)) {
                        cache.put(1, 1);
                        tx0.commit();
                    }
                    assertEquals(TransactionState.ROLLED_BACK, tx.state());
                    return null;
                }
            }, IgniteTxHeuristicCheckedException.class);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 2 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class IgnitePessimisticTxSuspendResumeTest method testSuspendPessimisticTx.

/**
 * Test for suspension on pessimistic transaction.
 *
 * @throws Exception If failed.
 */
public void testSuspendPessimisticTx() throws Exception {
    try (Ignite g = startGrid()) {
        IgniteCache<Integer, String> cache = jcache();
        IgniteTransactions txs = g.transactions();
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            final Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, isolation);
            cache.put(1, "1");
            GridTestUtils.assertThrowsWithCause(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    tx.suspend();
                    return null;
                }
            }, UnsupportedOperationException.class);
            tx.close();
            assertNull(cache.get(1));
        }
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Example 3 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class PerformingTransactions method executingTransactionsExample.

public static void executingTransactionsExample() {
    try (Ignite i = Ignition.start()) {
        CacheConfiguration<String, Integer> cfg = new CacheConfiguration<>();
        cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cfg.setName("myCache");
        IgniteCache<String, Integer> cache = i.getOrCreateCache("myCache");
        cache.put("Hello", 1);
        // tag::executing[]
        Ignite ignite = Ignition.ignite();
        IgniteTransactions transactions = ignite.transactions();
        try (Transaction tx = transactions.txStart()) {
            Integer hello = cache.get("Hello");
            if (hello == 1)
                cache.put("Hello", 11);
            cache.put("World", 22);
            tx.commit();
        }
        // end::executing[]
        System.out.println(cache.get("Hello"));
        System.out.println(cache.get("World"));
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 4 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method lockKey.

/**
 * @param releaseLatch Release lock latch.
 * @param cache Cache.
 * @param key Key.
 * @return Future.
 * @throws Exception If failed.
 */
private IgniteInternalFuture<?> lockKey(final CountDownLatch releaseLatch, final IgniteCache<Integer, Integer> cache, final Integer key) throws Exception {
    final CountDownLatch lockLatch = new CountDownLatch(1);
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.put(key, 1);
                log.info("Locked key: " + key);
                lockLatch.countDown();
                assertTrue(releaseLatch.await(100000, SECONDS));
                log.info("Commit tx: " + key);
                tx.commit();
            }
            return null;
        }
    }, "lock-thread");
    assertTrue(lockLatch.await(10, SECONDS));
    return fut;
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 5 with IgniteTransactions

use of org.apache.ignite.IgniteTransactions in project ignite by apache.

the class CacheSerializableTransactionsTest method txStreamerLoad.

/**
 * @param ignite Node.
 * @param key Key.
 * @param cacheName Cache name.
 * @param allowOverwrite Streamer flag.
 * @throws Exception If failed.
 */
private void txStreamerLoad(Ignite ignite, Integer key, String cacheName, boolean allowOverwrite) {
    IgniteCache<Integer, Integer> cache = ignite.cache(cacheName);
    log.info("Test key: " + key);
    Integer loadVal = -1;
    IgniteTransactions txs = ignite.transactions();
    try (IgniteDataStreamer<Integer, Integer> streamer = ignite.dataStreamer(cache.getName())) {
        streamer.allowOverwrite(allowOverwrite);
        streamer.addData(key, loadVal);
    }
    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
        Integer val = cache.get(key);
        assertEquals(loadVal, val);
        tx.commit();
    }
    checkValue(key, loadVal, cache.getName());
    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
        Integer val = cache.get(key);
        assertEquals(loadVal, val);
        cache.put(key, 0);
        tx.commit();
    }
    checkValue(key, 0, cache.getName());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

IgniteTransactions (org.apache.ignite.IgniteTransactions)120 Transaction (org.apache.ignite.transactions.Transaction)116 Ignite (org.apache.ignite.Ignite)87 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 Test (org.junit.Test)49 IgniteCache (org.apache.ignite.IgniteCache)36 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)29 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)28 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)28 HashMap (java.util.HashMap)26 CacheException (javax.cache.CacheException)26 ArrayList (java.util.ArrayList)23 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)23 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)19 Map (java.util.Map)18 IgniteException (org.apache.ignite.IgniteException)18 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 HashSet (java.util.HashSet)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)16