use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class ClientXATest method testRollback.
@Test
public void testRollback() throws Exception {
Hazelcast.newHazelcastInstance();
HazelcastInstance client = HazelcastClient.newHazelcastClient();
HazelcastXAResource xaResource = client.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(client.getMap("m").get("key"));
}
use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class ClientXATest method testRecovery.
@Test
public void testRecovery() throws Exception {
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
Hazelcast.newHazelcastInstance();
Hazelcast.newHazelcastInstance();
HazelcastXAResource xaResource = instance.getXAResource();
Xid myXid = new SerializableXID(42, "globalTransactionId".getBytes(), "branchQualifier".getBytes());
xaResource.start(myXid, 0);
TransactionContext context1 = xaResource.getTransactionContext();
TransactionalMap<Object, Object> map = context1.getMap("map");
map.put("key", "value");
xaResource.prepare(myXid);
instance.shutdown();
HazelcastInstance client = HazelcastClient.newHazelcastClient();
HazelcastXAResource clientXaResource = client.getXAResource();
Xid[] recovered = clientXaResource.recover(0);
for (Xid xid : recovered) {
clientXaResource.commit(xid, false);
}
assertEquals("value", client.getMap("map").get("key"));
}
use of com.hazelcast.transaction.HazelcastXAResource in project hazelcast by hazelcast.
the class ClientXATest method txn.
private void txn(HazelcastInstance instance) throws Exception {
HazelcastXAResource xaResource = instance.getXAResource();
tm.begin();
Transaction transaction = tm.getTransaction();
transaction.enlistResource(xaResource);
boolean error = false;
try {
TransactionContext context = xaResource.getTransactionContext();
TransactionalMap m = context.getMap("m");
m.put(random.nextInt(10), "value");
} catch (Exception e) {
logger.severe("Exception during transaction", e);
error = true;
} finally {
close(error, xaResource);
}
}
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 HazelcastOSGiInstanceTest method getXAResourceCalledSuccessfullyOverOSGiInstance.
@Test
public void getXAResourceCalledSuccessfullyOverOSGiInstance() {
HazelcastXAResource mockXAResource = mock(HazelcastXAResource.class);
HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
HazelcastOSGiInstance hazelcastOSGiInstance = HazelcastOSGiTestUtil.createHazelcastOSGiInstance(mockHazelcastInstance);
when(mockHazelcastInstance.getXAResource()).thenReturn(mockXAResource);
assertEquals(mockXAResource, hazelcastOSGiInstance.getXAResource());
verify(mockHazelcastInstance).getXAResource();
}
Aggregations