use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class TxnMapNearCacheInvalidationTest method after_txn_commit_near_cache_should_be_invalidated.
@Test
public void after_txn_commit_near_cache_should_be_invalidated() {
Config cfg = getConfig();
String mapName = "cache";
MapConfig cacheConfig = cfg.getMapConfig(mapName);
NearCacheConfig nearCacheConfig = new NearCacheConfig();
nearCacheConfig.setInvalidateOnChange(true).setCacheLocalEntries(true).setInMemoryFormat(inMemoryFormat).setSerializeKeys(serializeKeys);
cacheConfig.setNearCacheConfig(nearCacheConfig);
HazelcastInstance server = createHazelcastInstance(cfg);
IMap map = server.getMap(mapName);
String key = "key";
String oldValue = "oldValue";
String updatedValue = "updatedValue";
// populate imap
map.put(key, oldValue);
// populate near cache
Object valueReadBeforeTxnFromNonTxnMap = map.get(key);
// begin txn
TransactionOptions opts = new TransactionOptions();
opts.setTransactionType(TransactionOptions.TransactionType.TWO_PHASE);
TransactionContext ctx = server.newTransactionContext(opts);
ctx.beginTransaction();
TransactionalMap txnMap = ctx.getMap(mapName);
Object valueReadInsideTxnFromTxnMapBeforeUpdate = txnMap.get(key);
txnMap.put(key, updatedValue);
Object valueReadInsideTxnFromTxnMapAfterUpdate = txnMap.get(key);
Object valueReadInsideTxnFromNonTxnMapAfterUpdate = map.get(key);
ctx.commitTransaction();
// check values read from txn map
assertEquals(oldValue, valueReadInsideTxnFromTxnMapBeforeUpdate);
assertEquals(updatedValue, valueReadInsideTxnFromTxnMapAfterUpdate);
// check values read from non-txn map
assertEquals(oldValue, valueReadBeforeTxnFromNonTxnMap);
assertEquals(oldValue, valueReadInsideTxnFromNonTxnMapAfterUpdate);
Object valueReadAfterTxnFromNonTxnMap = map.get(key);
assertEquals(updatedValue, valueReadAfterTxnFromNonTxnMap);
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnReplace_whenReplaceFails_keyShouldRemainUnlocked.
@Test
public void testTxnReplace_whenReplaceFails_keyShouldRemainUnlocked() throws InterruptedException {
HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
IMap<String, Object> map = hazelcastInstance.getMap(mapName);
hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
transactionalMap.replace(key, value);
return null;
}
});
assertFalse("Key remains locked!", map.isLocked(key));
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnReplace_whenReplaceIfSameFails_keyShouldRemainUnlocked.
@Test
public void testTxnReplace_whenReplaceIfSameFails_keyShouldRemainUnlocked() throws InterruptedException {
HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
IMap<String, Object> map = hazelcastInstance.getMap(mapName);
map.put(key, value);
hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
transactionalMap.replace(key, value + "other", value);
return null;
}
});
assertFalse("Key remains locked!", map.isLocked(key));
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnReplace_whenReplaceIfSameFails_keyShouldRemainLocked_whenExplicitlyLocked.
@Test
public void testTxnReplace_whenReplaceIfSameFails_keyShouldRemainLocked_whenExplicitlyLocked() throws InterruptedException {
final HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
final IMap<String, Object> map = hazelcastInstance.getMap(mapName);
hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
transactionalMap.getForUpdate(key);
boolean replace = transactionalMap.replace(key, value + "other", value);
assertFalse(replace);
assertTrue("Key remains unlocked!", map.isLocked(key));
return null;
}
});
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlocked.
@Test
public void testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlocked() throws InterruptedException {
HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
IMap<String, Object> map = hazelcastInstance.getMap(mapName);
map.put(key, value);
hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
transactionalMap.putIfAbsent(key, "t");
return null;
}
});
assertFalse("Key remains locked!", map.isLocked(key));
}
Aggregations