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