Search in sources :

Example 41 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 42 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 43 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 44 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 45 with TransactionalMap

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

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