Search in sources :

Example 71 with TransactionException

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

the class MapTransactionTest method testGetForUpdate.

@Test
public void testGetForUpdate() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap<String, String> map = h2.getMap("default");
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicBoolean pass = new AtomicBoolean(true);
    map.put("var", "value0");
    Runnable updater = new Runnable() {

        public void run() {
            try {
                latch1.await(100, TimeUnit.SECONDS);
                pass.set(map.tryPut("var", "value1", 0, TimeUnit.SECONDS) == false);
                latch2.countDown();
            } catch (Exception e) {
            }
        }
    };
    new Thread(updater).start();
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            try {
                final TransactionalMap<String, Integer> txMap = context.getMap("default");
                assertEquals("value0", txMap.getForUpdate("var"));
                latch1.countDown();
                latch2.await(100, TimeUnit.SECONDS);
            } catch (Exception e) {
            }
            return true;
        }
    });
    assertTrue(b);
    assertTrue(pass.get());
    assertTrue(map.tryPut("var", "value2", 0, TimeUnit.SECONDS));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TransactionException(com.hazelcast.transaction.TransactionException) ExecutionException(java.util.concurrent.ExecutionException) ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) TransactionNotActiveException(com.hazelcast.transaction.TransactionNotActiveException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 72 with TransactionException

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

the class MapTransactionTest method testTxnPutIfAbsent.

@Test
public void testTxnPutIfAbsent() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map1 = h1.getMap("default");
    final IMap map2 = h2.getMap("default");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.putIfAbsent("1", "value");
            assertEquals("value", txMap.putIfAbsent("1", "value2"));
            assertEquals("value", txMap.get("1"));
            assertNull(map1.get("1"));
            assertNull(map2.get("2"));
            return true;
        }
    });
    assertTrue(b);
    assertEquals("value", map1.get("1"));
    assertEquals("value", map2.get("1"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 73 with TransactionException

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

the class MapTransactionTest method testRemove.

@Test
public void testRemove() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map1 = h1.getMap("default");
    final IMap map2 = h2.getMap("default");
    map2.put("1", "1");
    map2.put("2", "2");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.put("3", "3");
            map2.put("4", "4");
            assertEquals("1", txMap.remove("1"));
            assertEquals("2", map1.remove("2"));
            assertEquals("1", map1.get("1"));
            assertEquals(null, txMap.get("1"));
            assertEquals(null, txMap.remove("2"));
            assertEquals(2, txMap.size());
            return true;
        }
    });
    assertTrue(b);
    assertEquals(null, map1.get("1"));
    assertEquals(null, map2.get("1"));
    assertEquals(null, map1.get("2"));
    assertEquals(null, map2.get("2"));
    assertEquals("3", map1.get("3"));
    assertEquals("3", map2.get("3"));
    assertEquals("4", map1.get("4"));
    assertEquals("4", map2.get("4"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 74 with TransactionException

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

the class TransactionQueueTest method testTransactionalOfferPoll1.

@Test
public void testTransactionalOfferPoll1() throws Exception {
    Config config = new Config();
    final int insCount = 4;
    final String name = "defQueue";
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final TransactionContext context = instances[0].newTransactionContext();
    context.beginTransaction();
    try {
        TransactionalQueue<String> q = context.getQueue(name);
        assertTrue(q.offer("ali"));
        String s = q.poll();
        assertEquals("ali", s);
        context.commitTransaction();
    } catch (TransactionException e) {
        context.rollbackTransaction();
        throw e;
    }
    assertEquals(0, getQueue(instances, name).size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 75 with TransactionException

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

the class XATransactionCommitMessageTask method call.

@Override
protected Object call() throws Exception {
    String transactionId = parameters.transactionId;
    TransactionContext transactionContext = endpoint.getTransactionContext(transactionId);
    if (transactionContext == null) {
        throw new TransactionException("No transaction context with given transactionId: " + transactionId);
    }
    Transaction transaction = TransactionAccessor.getTransaction(transactionContext);
    if (parameters.onePhase) {
        transaction.prepare();
    }
    transaction.commit();
    endpoint.removeTransactionContext(transactionId);
    return null;
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) Transaction(com.hazelcast.transaction.impl.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext)

Aggregations

TransactionException (com.hazelcast.transaction.TransactionException)91 Test (org.junit.Test)74 HazelcastInstance (com.hazelcast.core.HazelcastInstance)68 QuickTest (com.hazelcast.test.annotation.QuickTest)66 ParallelTest (com.hazelcast.test.annotation.ParallelTest)62 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)62 TransactionalMap (com.hazelcast.core.TransactionalMap)60 NightlyTest (com.hazelcast.test.annotation.NightlyTest)51 Config (com.hazelcast.config.Config)39 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)39 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)35 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.core.IMap)18 TransactionContext (com.hazelcast.transaction.TransactionContext)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 TransactionOptions (com.hazelcast.transaction.TransactionOptions)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5