Search in sources :

Example 6 with TransactionalMultiMap

use of com.hazelcast.transaction.TransactionalMultiMap in project hazelcast by hazelcast.

the class TxnMultiMapTest method testTxnCommit.

@Test
public void testTxnCommit() throws TransactionException {
    final String map1 = "map1";
    final String map2 = "map2";
    final String key = "1";
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance h1 = factory.newHazelcastInstance();
    HazelcastInstance h2 = factory.newHazelcastInstance();
    boolean success = h1.executeTransaction(new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMultiMap<String, String> txMap1 = context.getMultiMap(map1);
            TransactionalMultiMap<String, String> txMap2 = context.getMultiMap(map2);
            assertTrue(txMap1.put(key, "value1"));
            Object value1 = getSingleValue(txMap1, key);
            assertEquals("value1", value1);
            assertTrue(txMap2.put(key, "value2"));
            Object value2 = getSingleValue(txMap2, key);
            assertEquals("value2", value2);
            return true;
        }
    });
    assertTrue(success);
    assertEquals("value1", getSingleValue(h1.<String, String>getMultiMap(map1), key));
    assertEquals("value1", getSingleValue(h2.<String, String>getMultiMap(map1), key));
    assertEquals("value2", getSingleValue(h1.<String, String>getMultiMap(map2), key));
    assertEquals("value2", getSingleValue(h2.<String, String>getMultiMap(map2), key));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with TransactionalMultiMap

use of com.hazelcast.transaction.TransactionalMultiMap in project hazelcast by hazelcast.

the class ClientTxnMultiMapTest method testConcrruentTxnPut.

@Test
public void testConcrruentTxnPut() throws Exception {
    final String mapName = randomString();
    final MultiMap multiMap = client.getMultiMap(mapName);
    final int threads = 10;
    final ExecutorService ex = Executors.newFixedThreadPool(threads);
    final CountDownLatch latch = new CountDownLatch(threads);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
    for (int i = 0; i < threads; i++) {
        final int key = i;
        ex.execute(new Runnable() {

            public void run() {
                multiMap.put(key, "value");
                final TransactionContext context = client.newTransactionContext();
                try {
                    context.beginTransaction();
                    final TransactionalMultiMap txnMultiMap = context.getMultiMap(mapName);
                    txnMultiMap.put(key, "value");
                    txnMultiMap.put(key, "value1");
                    txnMultiMap.put(key, "value2");
                    assertEquals(3, txnMultiMap.get(key).size());
                    context.commitTransaction();
                    assertEquals(3, multiMap.get(key).size());
                } catch (TransactionException e) {
                    error.compareAndSet(null, e);
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    try {
        latch.await(1, TimeUnit.MINUTES);
        assertNull(error.get());
    } finally {
        ex.shutdownNow();
    }
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with TransactionalMultiMap

use of com.hazelcast.transaction.TransactionalMultiMap in project hazelcast by hazelcast.

the class ClientTxnMultiMapTest method testRemove_whenBackedWithList.

@Test
public void testRemove_whenBackedWithList() throws Exception {
    final String mapName = multiMapBackedByList + randomString();
    final String key = "key";
    final String value = "value";
    final MultiMap multiMap = server.getMultiMap(mapName);
    multiMap.put(key, value);
    TransactionContext tx = client.newTransactionContext();
    tx.beginTransaction();
    TransactionalMultiMap mulitMapTxn = tx.getMultiMap(mapName);
    Collection c = mulitMapTxn.remove(key);
    assertFalse(c.isEmpty());
    tx.commitTransaction();
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) Collection(java.util.Collection) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with TransactionalMultiMap

use of com.hazelcast.transaction.TransactionalMultiMap in project hazelcast by hazelcast.

the class ClientTxnMultiMapTest method testRemoveAll.

@Test
public void testRemoveAll() throws Exception {
    final String mapName = randomString();
    final String key = "key";
    MultiMap multiMap = client.getMultiMap(mapName);
    for (int i = 0; i < 10; i++) {
        multiMap.put(key, i);
    }
    TransactionContext tx = client.newTransactionContext();
    tx.beginTransaction();
    TransactionalMultiMap txnMultiMap = tx.getMultiMap(mapName);
    txnMultiMap.remove(key);
    tx.commitTransaction();
    assertTrue(multiMap.get(key).isEmpty());
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 TransactionalMultiMap (com.hazelcast.transaction.TransactionalMultiMap)9 Test (org.junit.Test)9 MultiMap (com.hazelcast.multimap.MultiMap)8 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)8 TransactionContext (com.hazelcast.transaction.TransactionContext)8 TransactionException (com.hazelcast.transaction.TransactionException)2 Collection (java.util.Collection)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1