Search in sources :

Example 76 with TransactionalMap

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);
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionContext(com.hazelcast.transaction.TransactionContext) NearCacheConfig(com.hazelcast.config.NearCacheConfig) MapConfig(com.hazelcast.config.MapConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 77 with TransactionalMap

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));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 78 with TransactionalMap

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));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 79 with TransactionalMap

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;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 80 with TransactionalMap

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));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TransactionalMap (com.hazelcast.transaction.TransactionalMap)95 Test (org.junit.Test)77 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)76 QuickTest (com.hazelcast.test.annotation.QuickTest)76 HazelcastInstance (com.hazelcast.core.HazelcastInstance)63 TransactionException (com.hazelcast.transaction.TransactionException)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)55 NightlyTest (com.hazelcast.test.annotation.NightlyTest)42 Config (com.hazelcast.config.Config)39 TransactionContext (com.hazelcast.transaction.TransactionContext)38 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)37 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.map.IMap)28 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)27 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)6 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 Collection (java.util.Collection)6 ExecutionException (java.util.concurrent.ExecutionException)6