Search in sources :

Example 31 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testGetForUpdate_whenUpdateTxnFails.

@Test
public void testGetForUpdate_whenUpdateTxnFails() throws TransactionException {
    Config config = getConfig();
    config.setProperty(ClusterProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), "5000");
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap<String, String> map = h2.getMap("default");
    map.put("var", "value");
    map.lock("varLocked");
    TransactionOptions opts = new TransactionOptions();
    opts.setTimeout(1, TimeUnit.SECONDS);
    try {
        boolean b = h1.executeTransaction(opts, new TransactionalTask<Boolean>() {

            public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                final TransactionalMap<String, String> txMap = context.getMap("default");
                txMap.getForUpdate("var");
                throw new TransactionException();
            }
        });
    } catch (TransactionException e) {
    }
    assertFalse(map.isLocked("var"));
    assertTrue(map.isLocked("varLocked"));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 32 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testGet_LoadsKeyFromMapLoader_whenKeyExistsInDb.

@Test
public void testGet_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
    final String mapName = randomMapName();
    final String valueFromDB = randomString();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(valueFromDB);
    Config config = getConfig();
    MapStoreConfig storeConfig = new MapStoreConfig();
    storeConfig.setEnabled(true).setImplementation(mock);
    config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    instance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> map = context.getMap(mapName);
            Object value = map.get(1);
            assertEquals(valueFromDB, value);
            verify(mock, times(1)).load(anyObject());
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 33 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testSet_whenNullValue.

@Test(expected = NullPointerException.class)
public void testSet_whenNullValue() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.set("key", null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 34 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testTxnCommit.

@Test
public void testTxnCommit() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final String map1 = "map1";
    final String map2 = "map2";
    final String key = "1";
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap1 = context.getMap(map1);
            final TransactionalMap<Object, Object> txMap2 = context.getMap(map2);
            txMap1.put(key, "value");
            assertEquals("value", txMap1.put(key, "value1"));
            assertEquals("value1", txMap1.get(key));
            txMap2.put(key, "value");
            assertEquals("value", txMap2.put(key, "value2"));
            assertEquals("value2", txMap2.get(key));
            assertTrue(txMap1.containsKey(key));
            assertTrue(txMap2.containsKey(key));
            assertNull(h1.getMap(map1).get(key));
            assertNull(h1.getMap(map2).get(key));
            return true;
        }
    });
    assertTrue(b);
    assertEquals("value1", h1.getMap(map1).get(key));
    assertEquals("value1", h2.getMap(map1).get(key));
    assertEquals("value2", h1.getMap(map2).get(key));
    assertEquals("value2", h2.getMap(map2).get(key));
    assertFalse(h1.getMap(map1).isLocked(key));
    assertFalse(h1.getMap(map2).isLocked(key));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 35 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testCommitOrder.

@Test
public void testCommitOrder() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(4);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final HazelcastInstance h3 = factory.newHazelcastInstance(config);
    final HazelcastInstance h4 = factory.newHazelcastInstance(config);
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.put("1", "value1");
            assertEquals("value1", txMap.put("1", "value2"));
            assertEquals("value2", txMap.put("1", "value3"));
            assertEquals("value3", txMap.put("1", "value4"));
            assertEquals("value4", txMap.put("1", "value5"));
            assertEquals("value5", txMap.put("1", "value6"));
            assertEquals(1, txMap.size());
            return true;
        }
    });
    assertEquals("value6", h4.getMap("default").get("1"));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) 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