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