Search in sources :

Example 81 with TransactionalMap

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

Example 82 with TransactionalMap

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());
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionException(com.hazelcast.transaction.TransactionException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionException(com.hazelcast.transaction.TransactionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 83 with TransactionalMap

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));
}
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 84 with TransactionalMap

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();
}
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 85 with TransactionalMap

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();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionException(com.hazelcast.transaction.TransactionException) 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)

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