Search in sources :

Example 11 with HazelcastXAResource

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

the class HazelcastXATest method testRecovery.

@Test
public void testRecovery() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(4);
    HazelcastInstance instance1 = factory.newHazelcastInstance();
    HazelcastInstance instance2 = factory.newHazelcastInstance();
    HazelcastInstance instance3 = factory.newHazelcastInstance();
    HazelcastXAResource xaResource = instance1.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);
    instance1.shutdown();
    instance1 = factory.newHazelcastInstance();
    xaResource = instance1.getXAResource();
    Xid[] recovered = xaResource.recover(0);
    for (Xid xid : recovered) {
        xaResource.commit(xid, false);
    }
    assertEquals("value", instance2.getMap("map").get("key"));
}
Also used : Xid(javax.transaction.xa.Xid) SerializableXID(com.hazelcast.transaction.impl.xa.SerializableXID) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with HazelcastXAResource

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

the class HazelcastXATest method recoverTX.

private void recoverTX(HazelcastInstance instance) throws XAException {
    HazelcastXAResource xaResource = instance.getXAResource();
    final Xid[] recovered = xaResource.recover(XAResource.TMNOFLAGS);
    for (Xid xid : recovered) {
        xaResource.commit(xid, false);
    }
}
Also used : Xid(javax.transaction.xa.Xid) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource)

Example 13 with HazelcastXAResource

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Transaction(javax.transaction.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) RollbackException(javax.transaction.RollbackException) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Aggregations

HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)13 TransactionContext (com.hazelcast.transaction.TransactionContext)9 Test (org.junit.Test)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 Transaction (javax.transaction.Transaction)6 TransactionalMap (com.hazelcast.core.TransactionalMap)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 SlowTest (com.hazelcast.test.annotation.SlowTest)4 SystemException (javax.transaction.SystemException)4 Xid (javax.transaction.xa.Xid)4 SerializableXID (com.hazelcast.transaction.impl.xa.SerializableXID)3 RollbackException (javax.transaction.RollbackException)3 XAException (javax.transaction.xa.XAException)3 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)1 NightlyTest (com.hazelcast.test.annotation.NightlyTest)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 ExecutorService (java.util.concurrent.ExecutorService)1