Search in sources :

Example 21 with Transaction

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

the class SpringTransactionManager method doRollback.

/**
 * {@inheritDoc}
 */
@Override
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
    IgniteTransactionObject txObj = (IgniteTransactionObject) status.getTransaction();
    Transaction tx = txObj.getTransactionHolder().getTransaction();
    if (status.isDebug() && log.isDebugEnabled())
        log.debug("Rolling back Ignite transaction: " + tx);
    try {
        tx.rollback();
    } catch (IgniteException e) {
        throw new TransactionSystemException("Could not rollback Ignite transaction", e);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) TransactionSystemException(org.springframework.transaction.TransactionSystemException)

Example 22 with Transaction

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

the class SpringTransactionManager method doBegin.

/**
 * {@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);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) IgniteException(org.apache.ignite.IgniteException) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException)

Example 23 with Transaction

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

the class CacheHibernateStoreExample method executeTransaction.

/**
 * Executes transaction with read/write-through to persistent store.
 *
 * @param cache Cache to execute transaction on.
 */
private static void executeTransaction(IgniteCache<Long, Person> cache) {
    try (Transaction tx = Ignition.ignite().transactions().txStart()) {
        Person val = cache.get(id);
        System.out.println("Read value: " + val);
        val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
        System.out.println("Overwrote old value: " + val);
        val = cache.get(id);
        System.out.println("Read value: " + val);
        tx.commit();
    }
    System.out.println("Read value after commit: " + cache.get(id));
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Person(org.apache.ignite.examples.model.Person)

Example 24 with Transaction

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

the class IgniteDiagnosticMessagesTest method testSeveralLongRunningTxs.

/**
 * @throws Exception If failed.
 */
public void testSeveralLongRunningTxs() throws Exception {
    int timeout = 3500;
    System.setProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT, String.valueOf(timeout));
    try {
        testSpi = true;
        startGrid(0);
        GridStringLogger strLog = this.strLog = new GridStringLogger();
        strLog.logLength(1024 * 100);
        startGrid(1);
        awaitPartitionMapExchange();
        CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        ccfg.setWriteSynchronizationMode(FULL_SYNC);
        ccfg.setCacheMode(PARTITIONED);
        ccfg.setAtomicityMode(TRANSACTIONAL);
        final Ignite node0 = ignite(0);
        final Ignite node1 = ignite(1);
        node0.createCache(ccfg);
        UUID id0 = node0.cluster().localNode().id();
        TestRecordingCommunicationSpi.spi(node0).blockMessages(GridNearLockResponse.class, node1.name());
        IgniteCache<Object, Object> cache = node0.cache(DEFAULT_CACHE_NAME);
        int txCnt = 4;
        final List<Integer> keys = primaryKeys(cache, txCnt, 0);
        final AtomicInteger idx = new AtomicInteger();
        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteCache<Object, Object> cache = node1.cache(DEFAULT_CACHE_NAME);
                try (Transaction tx = node1.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    Integer key = keys.get(idx.getAndIncrement() % keys.size());
                    cache.putIfAbsent(key, String.valueOf(key));
                    tx.commit();
                }
                return null;
            }
        }, txCnt * 2, "tx");
        U.sleep(timeout * 2);
        assertFalse(fut.isDone());
        TestRecordingCommunicationSpi.spi(node0).stopBlock();
        fut.get();
        String log = strLog.toString();
        assertTrue(log.contains("Cache entries [cacheId=" + CU.cacheId(DEFAULT_CACHE_NAME) + ", cacheName=" + DEFAULT_CACHE_NAME + "]:"));
        assertTrue(countTxKeysInASingleBlock(log) == txCnt);
        assertTrue(log.contains("General node info [id=" + id0));
    } finally {
        System.clearProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT);
    }
}
Also used : GridStringLogger(org.apache.ignite.testframework.GridStringLogger) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 25 with Transaction

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

the class GridCacheDaemonNodeAbstractSelfTest method testExplicit.

/**
 * @throws Exception If failed.
 */
public void testExplicit() throws Exception {
    try {
        startGridsMultiThreaded(3);
        daemon = true;
        startGrid(4);
        IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
        for (int i = 0; i < 30; i++) {
            try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                cache.put(i, i);
                tx.commit();
            }
        }
        Map<Integer, Integer> batch = new HashMap<>();
        for (int i = 30; i < 60; i++) batch.put(i, i);
        try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            cache.putAll(batch);
            tx.commit();
        }
        for (int i = 0; i < 60; i++) assertEquals(i, (int) cache.get(i));
    } finally {
        stopAllGrids();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) HashMap(java.util.HashMap)

Aggregations

Transaction (org.apache.ignite.transactions.Transaction)493 Ignite (org.apache.ignite.Ignite)204 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)183 IgniteCache (org.apache.ignite.IgniteCache)88 IgniteTransactions (org.apache.ignite.IgniteTransactions)78 IgniteException (org.apache.ignite.IgniteException)74 CacheException (javax.cache.CacheException)60 HashMap (java.util.HashMap)54 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)45 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)44 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)42 ArrayList (java.util.ArrayList)41 Callable (java.util.concurrent.Callable)41 Map (java.util.Map)36 IgniteEx (org.apache.ignite.internal.IgniteEx)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 IgniteKernal (org.apache.ignite.internal.IgniteKernal)30 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)30 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29