Search in sources :

Example 51 with TransactionContext

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

the class TransactionalSetAbstractTest method testSetSizeAfterRemove_withinTxn.

@Test
public void testSetSizeAfterRemove_withinTxn() throws Exception {
    set.add(ELEMENT);
    TransactionContext context = local.newTransactionContext();
    context.beginTransaction();
    TransactionalSet<Object> txnSet = context.getSet(setName);
    txnSet.remove(ELEMENT);
    context.commitTransaction();
    assertEquals(0, set.size());
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext) Test(org.junit.Test)

Example 52 with TransactionContext

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

the class TransactionalSetAbstractTest method testAddRollBack.

@Test
public void testAddRollBack() throws Exception {
    set.add(ELEMENT);
    TransactionContext context = local.newTransactionContext();
    context.beginTransaction();
    TransactionalSet<Object> setTxn = context.getSet(setName);
    setTxn.add("itemWillGetRollBacked");
    context.rollbackTransaction();
    assertEquals(1, set.size());
}
Also used : TransactionContext(com.hazelcast.transaction.TransactionContext) Test(org.junit.Test)

Example 53 with TransactionContext

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

the class TransactionQueueTest method testOrder_WhenMultipleConcurrentTransactionRollback.

@Test
public void testOrder_WhenMultipleConcurrentTransactionRollback() throws InterruptedException {
    final HazelcastInstance instance = createHazelcastInstance();
    final String name = randomString();
    IQueue<Integer> queue = instance.getQueue(name);
    queue.offer(1);
    queue.offer(2);
    queue.offer(3);
    TransactionContext firstContext = instance.newTransactionContext();
    firstContext.beginTransaction();
    firstContext.getQueue(name).poll();
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread() {

        @Override
        public void run() {
            TransactionContext secondContext = instance.newTransactionContext();
            secondContext.beginTransaction();
            secondContext.getQueue(name).poll();
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            secondContext.rollbackTransaction();
        }
    };
    thread.start();
    firstContext.rollbackTransaction();
    latch.countDown();
    thread.join();
    assertEquals(1, queue.poll().intValue());
    assertEquals(2, queue.poll().intValue());
    assertEquals(3, queue.poll().intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 54 with TransactionContext

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

the class TransactionQueueTest method testPeekMethod.

@Test
public void testPeekMethod() throws Exception {
    final Config config = new Config();
    final int insCount = 4;
    final String name = "defQueue";
    final 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);
        final String response1 = q.peek(10, SECONDS);
        assertNull(response1);
        assertTrue(q.offer("ali"));
        final String response2 = q.peek();
        assertEquals("ali", response2);
        context.commitTransaction();
    } catch (TransactionException e) {
        context.rollbackTransaction();
        throw e;
    }
    assertEquals(1, 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 55 with TransactionContext

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

the class TransactionQueueTest method issue_6259_backupNotRollingBackCorrectly.

// https://github.com/hazelcast/hazelcast/issues/6259
@Test
public void issue_6259_backupNotRollingBackCorrectly() {
    HazelcastInstance[] cluster = createHazelcastInstanceFactory(2).newInstances();
    HazelcastInstance local = cluster[0];
    HazelcastInstance remote = cluster[1];
    final String queueName = generateKeyOwnedBy(remote);
    // first we add an item
    local.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalQueue<String> queue = context.getQueue(queueName);
            queue.offer("item");
            return null;
        }
    });
    // we remove the item and then do a rollback. This causes the local (backup) to become out
    // of sync with the remote (primary)
    TransactionContext firstCtxt = local.newTransactionContext();
    firstCtxt.beginTransaction();
    TransactionalQueue<String> queue = firstCtxt.getQueue(queueName);
    queue.poll();
    firstCtxt.rollbackTransaction();
    // we kill the remote. Now the local (which was the backup) is going to become primary
    remote.shutdown();
    // if we take the item, we should get an error
    TransactionContext secondCtxt = local.newTransactionContext();
    secondCtxt.beginTransaction();
    queue = secondCtxt.getQueue(queueName);
    String found = queue.poll();
    assertEquals("item", found);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionContext (com.hazelcast.transaction.TransactionContext)280 Test (org.junit.Test)209 QuickTest (com.hazelcast.test.annotation.QuickTest)189 ParallelTest (com.hazelcast.test.annotation.ParallelTest)184 HazelcastInstance (com.hazelcast.core.HazelcastInstance)58 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)48 TransactionalMap (com.hazelcast.core.TransactionalMap)33 TransactionException (com.hazelcast.transaction.TransactionException)22 IMap (com.hazelcast.core.IMap)21 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)19 Config (com.hazelcast.config.Config)17 NightlyTest (com.hazelcast.test.annotation.NightlyTest)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 XAException (javax.transaction.xa.XAException)14 TransactionalQueue (com.hazelcast.core.TransactionalQueue)13 TransactionalMultiMap (com.hazelcast.core.TransactionalMultiMap)9 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)9 ExecutionException (java.util.concurrent.ExecutionException)9 ISet (com.hazelcast.core.ISet)8 MultiMap (com.hazelcast.core.MultiMap)8