use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictReplace.
/**
* @throws Exception If failed.
*/
public void testTxConflictReplace() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (final Integer key : keys) {
log.info("Test key: " + key);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertFalse(replace);
updateKey(cache, key, 1);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertFalse(replace);
tx.commit();
}
checkValue(key, null, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertFalse(replace);
updateKey(cache, key, 3);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 3, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertTrue(replace);
txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.remove(key);
return null;
}
});
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, null, cache.getName());
cache.put(key, 1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method rollbackIfLockedPartialLock.
/**
* @param locKey If {@code true} gets lock for local key.
* @throws Exception If failed.
*/
private void rollbackIfLockedPartialLock(boolean locKey) throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
final Integer key1 = primaryKey(ignite(1).cache(cache.getName()));
final Integer key2 = locKey ? primaryKey(cache) : primaryKey(ignite(2).cache(cache.getName()));
CountDownLatch latch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = lockKey(latch, cache, key1);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, 2);
cache.put(key2, 2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
latch.countDown();
fut.get();
checkValue(key1, 1, cache.getName());
checkValue(key2, null, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, 2);
cache.put(key2, 2);
tx.commit();
}
checkValue(key1, 2, cache.getName());
checkValue(key2, 2, cache.getName());
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testReadLockPessimisticTxConflict.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("UnnecessaryLocalVariable")
public void testReadLockPessimisticTxConflict() throws Exception {
Ignite ignite0 = ignite(0);
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
ignite0.createCache(ccfg);
try {
Ignite ignite = ignite0;
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
Integer writeKey = Integer.MAX_VALUE;
List<Integer> readKeys = testKeys(cache);
for (Integer readKey : readKeys) {
CountDownLatch latch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = lockKey(latch, cache, readKey);
try {
// No conflict for write, conflict with pessimistic tx for read.
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(writeKey, writeKey);
cache.get(readKey);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
} finally {
latch.countDown();
}
fut.get();
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxRollbackIfLocked1.
/**
* @throws Exception If failed.
*/
public void testTxRollbackIfLocked1() throws Exception {
Ignite ignite0 = ignite(0);
IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (Integer key : keys) {
log.info("Test key: " + key);
CountDownLatch latch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = lockKey(latch, cache, key);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, 2);
log.info("Commit");
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
latch.countDown();
fut.get();
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, 2);
tx.commit();
}
checkValue(key, 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.transactions.TransactionOptimisticException in project ignite by apache.
the class CacheSerializableTransactionsTest method rollbackNearCacheRead.
/**
* @param near If {@code true} updates entry using the same near cache.
* @throws Exception If failed.
*/
private void rollbackNearCacheRead(boolean near) throws Exception {
Ignite ignite0 = ignite(0);
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false));
final String cacheName = cache0.getName();
try {
Ignite ignite = ignite(SRVS);
IgniteCache<Integer, Integer> cache = ignite.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>());
IgniteTransactions txs = ignite.transactions();
Integer key1 = primaryKey(ignite(0).cache(cacheName));
Integer key2 = primaryKey(ignite(1).cache(cacheName));
Integer key3 = primaryKey(ignite(2).cache(cacheName));
cache0.put(key1, -1);
cache0.put(key2, -1);
cache0.put(key3, -1);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(key1);
cache.get(key2);
cache.get(key3);
updateKey(near ? cache : cache0, key2, -2);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key1, -1, cacheName);
checkValue(key2, -2, cacheName);
checkValue(key3, -1, cacheName);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key1, key1);
cache.put(key2, key2);
cache.put(key3, key3);
tx.commit();
}
checkValue(key1, key1, cacheName);
checkValue(key2, key2, cacheName);
checkValue(key3, key3, cacheName);
} finally {
destroyCache(cacheName);
}
}
Aggregations