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());
}
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());
}
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());
}
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());
}
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);
}
Aggregations