use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class ClientXATest method testWhenLockedOutOfTransaction.
@Test
public void testWhenLockedOutOfTransaction() throws Exception {
Hazelcast.newHazelcastInstance();
HazelcastInstance client = HazelcastClient.newHazelcastClient();
IMap<Object, Object> map = client.getMap("map");
map.put("key", "value");
HazelcastXAResource xaResource = client.getXAResource();
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
TransactionContext context = xaResource.getTransactionContext();
TransactionalMap<Object, Object> transactionalMap = context.getMap("map");
if (map.tryLock("key")) {
transactionalMap.remove("key");
}
tm.commit();
}
use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class ClientXATest method testRollbackOnTimeout.
@Test
public void testRollbackOnTimeout() throws Exception {
Hazelcast.newHazelcastInstance();
HazelcastInstance client = HazelcastClient.newHazelcastClient();
String name = randomString();
IQueue<Object> queue = client.getQueue(name);
queue.offer(randomString());
HazelcastXAResource xaResource = client.getXAResource();
tm.setTransactionTimeout(3);
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
TransactionContext context = xaResource.getTransactionContext();
try {
context.getQueue(name).take();
sleepAtLeastSeconds(5);
tm.commit();
fail();
} catch (RollbackException ignored) {
// Transaction already rolled-back due to timeout, no need to call tm.rollback explicitly
}
assertEquals("Queue size should be 1", 1, queue.size());
}
use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class HazelcastXATest method startTX.
private void startTX(final HazelcastInstance instance, final CountDownLatch nodeShutdownLatch) {
new Thread(new Runnable() {
@Override
public void run() {
try {
HazelcastXAResource xaResource = instance.getXAResource();
Xid xid = new SerializableXID(42, "globalTransactionId".getBytes(), "branchQualifier".getBytes());
xaResource.start(xid, XAResource.TMNOFLAGS);
TransactionContext context = xaResource.getTransactionContext();
final TransactionalMap<Object, Object> map = context.getMap("map");
map.put("key", "value");
xaResource.prepare(xid);
instance.shutdown();
nodeShutdownLatch.countDown();
} catch (XAException e) {
e.printStackTrace();
}
}
}).start();
}
use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class HazelcastXATest method testIsSame.
@Test
public void testIsSame() throws Exception {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = factory.newHazelcastInstance();
HazelcastInstance instance2 = factory.newHazelcastInstance();
HazelcastXAResource resource1 = instance1.getXAResource();
HazelcastXAResource resource2 = instance2.getXAResource();
assertTrue(resource1.isSameRM(resource2));
}
use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class HazelcastXATest method testRollback.
@Test
public void testRollback() throws Exception {
HazelcastInstance instance = createHazelcastInstance();
HazelcastXAResource xaResource = instance.getXAResource();
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
TransactionContext context = xaResource.getTransactionContext();
boolean error = false;
try {
final TransactionalMap m = context.getMap("m");
m.put("key", "value");
throw new RuntimeException("Exception for rolling back");
} catch (Exception e) {
error = true;
} finally {
close(error, xaResource);
}
assertNull(instance.getMap("m").get("key"));
}
Aggregations