Search in sources :

Example 1 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 2 with Transaction

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

the class CacheJdbcBlobStore method load.

/** {@inheritDoc} */
@SuppressWarnings({ "RedundantTypeArguments" })
@Override
public V load(K key) {
    init();
    Transaction tx = transaction();
    if (log.isDebugEnabled())
        log.debug("Store load [key=" + key + ", tx=" + tx + ']');
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = connection(tx);
        stmt = conn.prepareStatement(loadQry);
        stmt.setObject(1, toBytes(key));
        ResultSet rs = stmt.executeQuery();
        if (rs.next())
            return fromBytes(rs.getBytes(2));
    } catch (IgniteCheckedException | SQLException e) {
        throw new CacheLoaderException("Failed to load object: " + key, e);
    } finally {
        end(tx, conn, stmt);
    }
    return null;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with Transaction

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

the class CacheJdbcBlobStore method delete.

/** {@inheritDoc} */
@Override
public void delete(Object key) {
    init();
    Transaction tx = transaction();
    if (log.isDebugEnabled())
        log.debug("Store remove [key=" + key + ", tx=" + tx + ']');
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = connection(tx);
        stmt = conn.prepareStatement(delQry);
        stmt.setObject(1, toBytes(key));
        stmt.executeUpdate();
    } catch (IgniteCheckedException | SQLException e) {
        throw new CacheWriterException("Failed to remove object: " + key, e);
    } finally {
        end(tx, conn, stmt);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 4 with Transaction

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

the class GridCacheMarshallingNodeJoinSelfTest method testNodeJoin.

/**
     * @throws Exception If failed.
     */
public void testNodeJoin() throws Exception {
    final CountDownLatch allowJoin = new CountDownLatch(1);
    final CountDownLatch joined = new CountDownLatch(2);
    for (int i = 0; i < 2; i++) {
        ignite(i).events().localListen(new PE() {

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EventType.EVT_NODE_JOINED;
                info(">>> Event: " + evt);
                joined.countDown();
                return true;
            }
        }, EventType.EVT_NODE_JOINED);
    }
    IgniteInternalFuture<?> oneMoreGrid = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            allowJoin.await();
            startGrid("oneMoreGrid");
            return null;
        }
    }, 1);
    IgniteCache<Integer, TestObject> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
    try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        cache.get(0);
        allowJoin.countDown();
        joined.await();
        assertNotNull(cache.get(1));
        tx.commit();
    }
    oneMoreGrid.get();
    assertNotNull(cache.get(1));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException) Transaction(org.apache.ignite.transactions.Transaction) PE(org.apache.ignite.internal.util.typedef.PE) Event(org.apache.ignite.events.Event)

Example 5 with Transaction

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

the class GridCacheMultiUpdateLockSelfTest method checkMultiUpdateLocks.

/**
     * @param nearEnabled Near enabled flag.
     * @throws Exception If failed.
     */
private void checkMultiUpdateLocks(boolean nearEnabled) throws Exception {
    this.nearEnabled = nearEnabled;
    startGrids(3);
    try {
        IgniteKernal g = (IgniteKernal) grid(0);
        GridCacheContext<Object, Object> cctx = g.internalCache(DEFAULT_CACHE_NAME).context();
        GridDhtCacheAdapter cache = nearEnabled ? cctx.near().dht() : cctx.colocated();
        AffinityTopologyVersion topVer = cache.beginMultiUpdate();
        IgniteInternalFuture<?> startFut;
        try {
            assertEquals(3, topVer.topologyVersion());
            final AtomicBoolean started = new AtomicBoolean();
            startFut = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    info(">>>> Starting grid.");
                    Ignite g4 = startGrid(4);
                    started.set(true);
                    IgniteCache<Object, Object> c = g4.cache(DEFAULT_CACHE_NAME);
                    info(">>>> Checking tx in new grid.");
                    try (Transaction tx = g4.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        assertEquals(2, c.get("a"));
                        assertEquals(4, c.get("b"));
                        assertEquals(6, c.get("c"));
                    }
                    return null;
                }
            }, 1);
            U.sleep(200);
            info(">>>> Checking grid has not started yet.");
            assertFalse(started.get());
            // Check we can proceed with transactions.
            IgniteCache<Object, Object> cache0 = g.cache(DEFAULT_CACHE_NAME);
            info(">>>> Checking tx commit.");
            Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
            try {
                cache0.put("a", 1);
                cache0.put("b", 2);
                cache0.put("c", 3);
                tx.commit();
            } finally {
                tx.close();
            }
            info(">>>> Checking grid still is not started");
            assertFalse(started.get());
            tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
            try {
                cache0.put("a", 2);
                cache0.put("b", 4);
                cache0.put("c", 6);
                tx.commit();
            } finally {
                tx.close();
            }
        } finally {
            info(">>>> Releasing multi update.");
            cache.endMultiUpdate();
        }
        info("Waiting for thread termination.");
        startFut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) Transaction(org.apache.ignite.transactions.Transaction) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Ignite(org.apache.ignite.Ignite) Callable(java.util.concurrent.Callable)

Aggregations

Transaction (org.apache.ignite.transactions.Transaction)425 Ignite (org.apache.ignite.Ignite)176 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)165 IgniteTransactions (org.apache.ignite.IgniteTransactions)75 IgniteCache (org.apache.ignite.IgniteCache)72 IgniteException (org.apache.ignite.IgniteException)66 HashMap (java.util.HashMap)47 CacheException (javax.cache.CacheException)45 TransactionOptimisticException (org.apache.ignite.transactions.TransactionOptimisticException)41 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)38 Map (java.util.Map)30 IgniteKernal (org.apache.ignite.internal.IgniteKernal)29 CountDownLatch (java.util.concurrent.CountDownLatch)28 ArrayList (java.util.ArrayList)27 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)27 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 IgniteEx (org.apache.ignite.internal.IgniteEx)25 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)24 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)24