Search in sources :

Example 6 with Transaction

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

the class IgniteCacheReplicatedQuerySelfTest method testDistributedQuery.

/**
 * @throws Exception If test failed.
 */
public void testDistributedQuery() throws Exception {
    final int keyCnt = 4;
    Transaction tx = ignite1.transactions().txStart();
    try {
        for (int i = 1; i <= keyCnt; i++) cache1.put(new CacheKey(i), new CacheValue(String.valueOf(i)));
        tx.commit();
        info("Committed transaction: " + tx);
    } catch (IgniteException e) {
        tx.rollback();
        throw e;
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cache2.size() == keyCnt && cache3.size() == keyCnt;
        }
    }, 5000);
    QueryCursor<Cache.Entry<CacheKey, CacheValue>> qry = cache1.query(new SqlQuery<CacheKey, CacheValue>(CacheValue.class, "val > 1 and val < 4"));
    // Distributed query.
    assertEquals(2, qry.getAll().size());
    // Create new query, old query cannot be modified after it has been executed.
    qry = cache3.query(new SqlQuery<CacheKey, CacheValue>(CacheValue.class, "val > 1 and val < 4").setLocal(true));
    // Tests execute on node.
    Iterator<Cache.Entry<CacheKey, CacheValue>> iter = qry.iterator();
    assert iter != null;
    assert iter.hasNext();
    iter.next();
    assert iter.hasNext();
    iter.next();
    assert !iter.hasNext();
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteException(org.apache.ignite.IgniteException)

Example 7 with Transaction

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

the class IgniteCacheReplicatedQuerySelfTest method doTestLocalQuery.

/**
 * @param loc Explicit query locality flag.
 * @throws Exception if failed.
 */
private void doTestLocalQuery(boolean loc) throws Exception {
    cache1.clear();
    Transaction tx = ignite1.transactions().txStart();
    try {
        cache1.put(new CacheKey(1), new CacheValue("1"));
        cache1.put(new CacheKey(2), new CacheValue("2"));
        cache1.put(new CacheKey(3), new CacheValue("3"));
        cache1.put(new CacheKey(4), new CacheValue("4"));
        tx.commit();
        info("Committed transaction: " + tx);
    } catch (IgniteException e) {
        tx.rollback();
        throw e;
    }
    checkLocalQueryResults(cache1, loc);
    checkLocalQueryResults(cache2, loc);
    checkLocalQueryResults(cache3, loc);
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException)

Example 8 with Transaction

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

the class AbstractCacheJtaSelfTest method testJta.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testJta() throws Exception {
    UserTransaction jtaTx = jotm.getUserTransaction();
    IgniteCache<String, Integer> cache = jcache();
    assert ignite(0).transactions().tx() == null;
    jtaTx.begin();
    try {
        assert ignite(0).transactions().tx() == null;
        assert cache.getAndPut("key", 1) == null;
        Transaction tx = ignite(0).transactions().tx();
        assert tx != null;
        assert tx.state() == ACTIVE;
        Integer one = 1;
        assertEquals(one, cache.get("key"));
        tx = ignite(0).transactions().tx();
        assert tx != null;
        assert tx.state() == ACTIVE;
        jtaTx.commit();
        assert ignite(0).transactions().tx() == null;
    } finally {
        if (jtaTx.getStatus() == Status.STATUS_ACTIVE)
            jtaTx.rollback();
    }
    assertEquals((Integer) 1, cache.get("key"));
}
Also used : UserTransaction(javax.transaction.UserTransaction) UserTransaction(javax.transaction.UserTransaction) Transaction(org.apache.ignite.transactions.Transaction)

Example 9 with Transaction

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

the class AbstractCacheJtaSelfTest method testAsyncOpAwait.

/**
 * @throws Exception If failed.
 */
public void testAsyncOpAwait() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    GridTestSafeThreadFactory factory = new GridTestSafeThreadFactory("JtaThread");
    final CountDownLatch latch = new CountDownLatch(1);
    Callable<Object> c = new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertNull(grid(0).transactions().tx());
            UserTransaction jtaTx = jotm.getUserTransaction();
            jtaTx.begin();
            try {
                cache.put("key1", 1);
                cache.putAsync("key", 1);
                assertEquals(grid(0).transactions().tx().state(), ACTIVE);
                latch.countDown();
                info("Before JTA commit.");
            } finally {
                jtaTx.commit();
            }
            info("After JTA commit.");
            assertEquals((Integer) 1, cache.get("key"));
            return null;
        }
    };
    Thread task = factory.newThread(c);
    try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
        cache.put("key", 0);
        task.start();
        latch.await();
        while (task.getState() != Thread.State.WAITING) factory.checkError();
        info("Before cache TX commit.");
        tx.commit();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UserTransaction(javax.transaction.UserTransaction) Transaction(org.apache.ignite.transactions.Transaction) GridTestSafeThreadFactory(org.apache.ignite.testframework.GridTestSafeThreadFactory) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable)

Example 10 with Transaction

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

the class DemoCachesLoadService method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(ServiceContext ctx) throws Exception {
    cachePool.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            try {
                IgniteCache<Integer, Employee> cacheEmployee = ignite.cache(EMPLOYEE_CACHE_NAME);
                if (cacheEmployee != null)
                    try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        for (int i = 0, n = 1; i < cnt; i++, n++) {
                            Integer id = rnd.nextInt(EMPL_CNT);
                            Integer depId = rnd.nextInt(DEP_CNT);
                            double r = rnd.nextDouble();
                            cacheEmployee.put(id, new Employee(id, depId, depId, "First name employee #" + n, "Last name employee #" + n, "Email employee #" + n, "Phone number employee #" + n, new java.sql.Date((long) (r * range)), "Job employee #" + n, 500 + AgentDemoUtils.round(r * 2000, 2)));
                            if (rnd.nextBoolean())
                                cacheEmployee.remove(rnd.nextInt(EMPL_CNT));
                            cacheEmployee.get(rnd.nextInt(EMPL_CNT));
                        }
                        if (rnd.nextInt(100) > 20)
                            tx.commit();
                    }
            } catch (Throwable e) {
                if (!e.getMessage().contains("cache is stopped"))
                    ignite.log().error("Cache write task execution error", e);
            }
        }
    }, 10, 3, TimeUnit.SECONDS);
    cachePool.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            try {
                IgniteCache<Integer, Car> cache = ignite.cache(CAR_CACHE_NAME);
                if (cache != null)
                    for (int i = 0; i < cnt; i++) {
                        Integer carId = rnd.nextInt(CAR_CNT);
                        cache.put(carId, new Car(carId, rnd.nextInt(PARK_CNT), "Car #" + (i + 1)));
                        if (rnd.nextBoolean())
                            cache.remove(rnd.nextInt(CAR_CNT));
                    }
            } catch (IllegalStateException ignored) {
            // No-op.
            } catch (Throwable e) {
                if (!e.getMessage().contains("cache is stopped"))
                    ignite.log().error("Cache write task execution error", e);
            }
        }
    }, 10, 3, TimeUnit.SECONDS);
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) Employee(org.apache.ignite.console.demo.model.Employee) Transaction(org.apache.ignite.transactions.Transaction) Car(org.apache.ignite.console.demo.model.Car)

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