Search in sources :

Example 6 with TransactionalMap

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());
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) SampleObjects(com.hazelcast.query.SampleObjects) TransactionContext(com.hazelcast.transaction.TransactionContext) SqlPredicate(com.hazelcast.query.SqlPredicate) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with TransactionalMap

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));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with TransactionalMap

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"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Transaction(javax.transaction.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 9 with TransactionalMap

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);
    }
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) Transaction(javax.transaction.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) RollbackException(javax.transaction.RollbackException) SystemException(javax.transaction.SystemException) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource)

Example 10 with TransactionalMap

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

the class TransactionalMapContainsKeyMessageTask method innerCall.

@Override
protected Object innerCall() throws Exception {
    final TransactionContext context = getEndpoint().getTransactionContext(parameters.txnId);
    final TransactionalMap map = context.getMap(parameters.name);
    return map.containsKey(parameters.key);
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) TransactionContext(com.hazelcast.transaction.TransactionContext)

Aggregations

TransactionalMap (com.hazelcast.core.TransactionalMap)92 Test (org.junit.Test)73 QuickTest (com.hazelcast.test.annotation.QuickTest)68 ParallelTest (com.hazelcast.test.annotation.ParallelTest)67 HazelcastInstance (com.hazelcast.core.HazelcastInstance)61 TransactionException (com.hazelcast.transaction.TransactionException)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)59 NightlyTest (com.hazelcast.test.annotation.NightlyTest)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 Config (com.hazelcast.config.Config)36 TransactionContext (com.hazelcast.transaction.TransactionContext)33 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)30 IMap (com.hazelcast.core.IMap)29 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)12 SqlPredicate (com.hazelcast.query.SqlPredicate)8 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 Collection (java.util.Collection)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)6 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6