use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class TransactionalMapReplaceIfSameMessageTask method innerCall.
@Override
protected Object innerCall() throws Exception {
final TransactionContext context = getEndpoint().getTransactionContext(parameters.txnId);
final TransactionalMap map = context.getMap(parameters.name);
return map.replace(parameters.key, parameters.oldValue, parameters.newValue);
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testGetForUpdate.
@Test
public void testGetForUpdate() throws TransactionException {
final String mapName = randomString();
final String key = "key";
final int initialValue = 111;
final int value = 888;
final CountDownLatch getKeyForUpdateLatch = new CountDownLatch(1);
final CountDownLatch afterTryPutResult = new CountDownLatch(1);
final IMap<String, Integer> map = client.getMap(mapName);
map.put(key, initialValue);
final AtomicBoolean tryPutResult = new AtomicBoolean(true);
Runnable incrementor = new Runnable() {
public void run() {
try {
getKeyForUpdateLatch.await(30, TimeUnit.SECONDS);
boolean result = map.tryPut(key, value, 0, TimeUnit.SECONDS);
tryPutResult.set(result);
afterTryPutResult.countDown();
} catch (Exception e) {
}
}
};
new Thread(incrementor).start();
client.executeTransaction(new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
try {
final TransactionalMap<String, Integer> txMap = context.getMap(mapName);
txMap.getForUpdate(key);
getKeyForUpdateLatch.countDown();
afterTryPutResult.await(30, TimeUnit.SECONDS);
} catch (Exception e) {
}
return true;
}
});
assertFalse(tryPutResult.get());
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testTnxMapRemoveKeyValue.
@Test
public void testTnxMapRemoveKeyValue() throws Exception {
final String mapName = randomString();
final String key1 = "key1";
final String oldValue1 = "old1";
final String key2 = "key2";
final String oldValue2 = "old2";
IMap map = client.getMap(mapName);
map.put(key1, oldValue1);
map.put(key2, oldValue2);
final TransactionContext context = client.newTransactionContext();
context.beginTransaction();
final TransactionalMap txMap = context.getMap(mapName);
txMap.remove(key1, oldValue1);
txMap.remove(key2, "NO_REMOVE_AS_NOT_VALUE");
context.commitTransaction();
assertNull(map.get(key1));
assertEquals(oldValue2, map.get(key2));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testTnxMapContainsKey.
@Test
public void testTnxMapContainsKey() throws Exception {
final String mapName = randomString();
IMap map = client.getMap(mapName);
map.put("key1", "value1");
final TransactionContext context = client.newTransactionContext();
context.beginTransaction();
final TransactionalMap txMap = context.getMap(mapName);
txMap.put("key2", "value2");
assertTrue(txMap.containsKey("key1"));
assertTrue(txMap.containsKey("key2"));
assertFalse(txMap.containsKey("key3"));
context.commitTransaction();
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testDeadLockFromClientInstance.
@Test
public void testDeadLockFromClientInstance() throws InterruptedException {
final String mapName = randomString();
final String key = "key";
final AtomicBoolean running = new AtomicBoolean(true);
Thread t = new Thread() {
public void run() {
while (running.get()) {
client.getMap(mapName).get(key);
}
}
};
t.start();
CBAuthorisation cb = new CBAuthorisation();
cb.setAmount(15000);
try {
TransactionContext context = client.newTransactionContext();
context.beginTransaction();
TransactionalMap mapTransaction = context.getMap(mapName);
// init data
mapTransaction.put(key, cb);
// start test deadlock, 3 set and concurrent, get deadlock
cb.setAmount(12000);
mapTransaction.set(key, cb);
cb.setAmount(10000);
mapTransaction.set(key, cb);
cb.setAmount(900);
mapTransaction.set(key, cb);
cb.setAmount(800);
mapTransaction.set(key, cb);
cb.setAmount(700);
mapTransaction.set(key, cb);
context.commitTransaction();
} catch (TransactionException e) {
e.printStackTrace();
fail();
}
running.set(false);
t.join();
}
Aggregations