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