use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testDuplicateValuesWithPredicates.
@Test
public void testDuplicateValuesWithPredicates() throws Exception {
final String mapName = randomString();
IMap map = client.getMap(mapName);
final SampleObjects.Employee emp1 = new SampleObjects.Employee("employee1", 10, true, 10D);
map.put("employee1", emp1);
final TransactionContext context = client.newTransactionContext();
context.beginTransaction();
final TransactionalMap txMap = context.getMap(mapName);
assertNull(txMap.put("employee1_repeated", emp1));
assertEquals(2, txMap.size());
assertEquals(2, txMap.keySet(new SqlPredicate("age = 10")).size());
assertEquals(2, txMap.values(new SqlPredicate("age = 10")).size());
context.commitTransaction();
assertEquals(2, map.keySet(new SqlPredicate("age = 10")).size());
assertEquals(2, map.values(new SqlPredicate("age = 10")).size());
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testTnxMapReplaceKeyValue.
@Test
public void testTnxMapReplaceKeyValue() throws Exception {
final String mapName = randomString();
final String key1 = "key1";
final String oldValue1 = "old1";
final String newValue1 = "new1";
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.replace(key1, oldValue1, newValue1);
txMap.replace(key2, "NOT_OLD_VALUE", "NEW_VALUE_CANT_BE_THIS");
context.commitTransaction();
assertEquals(newValue1, map.get(key1));
assertEquals(oldValue2, map.get(key2));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientXATest method testRollback.
@Test
public void testRollback() throws Exception {
Hazelcast.newHazelcastInstance();
HazelcastInstance client = HazelcastClient.newHazelcastClient();
HazelcastXAResource xaResource = client.getXAResource();
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
TransactionContext context = xaResource.getTransactionContext();
boolean error = false;
try {
final TransactionalMap m = context.getMap("m");
m.put("key", "value");
throw new RuntimeException("Exception for rolling back");
} catch (Exception e) {
error = true;
} finally {
close(error, xaResource);
}
assertNull(client.getMap("m").get("key"));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientXATest method txn.
private void txn(HazelcastInstance instance) throws Exception {
HazelcastXAResource xaResource = instance.getXAResource();
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
boolean error = false;
try {
TransactionContext context = xaResource.getTransactionContext();
TransactionalMap m = context.getMap("m");
m.put(random.nextInt(10), "value");
} catch (Exception e) {
logger.severe("Exception during transaction", e);
error = true;
} finally {
close(error, xaResource);
}
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnReplace_whenReplaceIfSameFails_keyShouldRemainUnlockedDuringTransaction.
@Test
public void testTxnReplace_whenReplaceIfSameFails_keyShouldRemainUnlockedDuringTransaction() 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);
boolean replace = transactionalMap.replace(key, value + "other", value);
assertFalse(replace);
assertFalse("Key remains locked!", map.isLocked(key));
return null;
}
});
}
Aggregations