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();
}
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);
}
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"));
}
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();
}
}
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);
}
Aggregations