Search in sources :

Example 71 with Xid

use of javax.transaction.xa.Xid in project geode by apache.

the class XAResourceAdaptor method testXAExceptionInCommit.

@Test
public void testXAExceptionInCommit() throws Exception {
    utx.begin();
    Thread thread = Thread.currentThread();
    Transaction txn = (Transaction) tm.getTransactionMap().get(thread);
    txn.registerSynchronization(new Synchronization() {

        public void beforeCompletion() {
        }

        public void afterCompletion(int status) {
            assertTrue(status == Status.STATUS_ROLLEDBACK);
        }
    });
    txn.enlistResource(new XAResourceAdaptor() {

        public void commit(Xid arg0, boolean arg1) throws XAException {
            throw new XAException(5);
        }

        public void rollback(Xid arg0) throws XAException {
            throw new XAException(6);
        }
    });
    try {
        utx.commit();
        fail("The commit should have thrown SystemException");
    } catch (SystemException expected) {
    // success
    }
    assertTrue(tm.getGlobalTransactionMap().isEmpty());
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 72 with Xid

use of javax.transaction.xa.Xid in project aries by apache.

the class XidFactoryImplTest method testAriesFactory.

@Test
public void testAriesFactory() throws Exception {
    XidFactory factory = new XidFactoryImpl("hi".getBytes());
    Xid id1 = factory.createXid();
    Xid id2 = factory.createXid();
    assertFalse("Should not match new: " + id1, factory.matchesGlobalId(id1.getGlobalTransactionId()));
    assertFalse("Should not match new: " + id2, factory.matchesGlobalId(id2.getGlobalTransactionId()));
    Xid b_id1 = factory.createBranch(id1, 1);
    Xid b_id2 = factory.createBranch(id2, 1);
    assertFalse("Should not match new branch: " + b_id1, factory.matchesBranchId(b_id1.getBranchQualifier()));
    assertFalse("Should not match new branch: " + b_id2, factory.matchesBranchId(b_id2.getBranchQualifier()));
    Thread.sleep(5);
    XidFactory factory2 = new XidFactoryImpl("hi".getBytes());
    assertTrue("Should match old: " + id1, factory2.matchesGlobalId(id1.getGlobalTransactionId()));
    assertTrue("Should match old: " + id2, factory2.matchesGlobalId(id2.getGlobalTransactionId()));
    assertTrue("Should match old branch: " + b_id1, factory2.matchesBranchId(b_id1.getBranchQualifier()));
    assertTrue("Should match old branch: " + b_id2, factory2.matchesBranchId(b_id2.getBranchQualifier()));
}
Also used : Xid(javax.transaction.xa.Xid) XidFactoryImpl(org.apache.aries.transaction.internal.XidFactoryImpl) XidFactory(org.apache.geronimo.transaction.manager.XidFactory) Test(org.junit.Test)

Example 73 with Xid

use of javax.transaction.xa.Xid in project aries by apache.

the class TransactionContextTest method testXAResourceRollbackOnly.

@Test
public void testXAResourceRollbackOnly() throws Exception {
    ctx.registerXAResource(xaResource, null);
    ctx.setRollbackOnly();
    Mockito.doAnswer(i -> {
        assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
        return null;
    }).when(xaResource).rollback(Mockito.any(Xid.class));
    ctx.finish();
    ArgumentCaptor<Xid> captor = ArgumentCaptor.forClass(Xid.class);
    InOrder inOrder = Mockito.inOrder(xaResource);
    inOrder.verify(xaResource).start(captor.capture(), Mockito.eq(XAResource.TMNOFLAGS));
    inOrder.verify(xaResource).setTransactionTimeout(Mockito.anyInt());
    inOrder.verify(xaResource).end(Mockito.eq(captor.getValue()), Mockito.eq(XAResource.TMFAIL));
    inOrder.verify(xaResource).rollback(Mockito.eq(captor.getValue()));
    Mockito.verifyNoMoreInteractions(xaResource);
}
Also used : Xid(javax.transaction.xa.Xid) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 74 with Xid

use of javax.transaction.xa.Xid in project aries by apache.

the class TransactionContextTest method testLastParticipantSuccessSoCommit.

@Test
public void testLastParticipantSuccessSoCommit() throws Exception {
    ctx.registerLocalResource(localResource);
    ctx.registerXAResource(xaResource, null);
    Mockito.doAnswer(i -> {
        assertEquals(COMMITTING, ctx.getTransactionStatus());
        return null;
    }).when(localResource).commit();
    Mockito.doAnswer(i -> {
        assertEquals(COMMITTING, ctx.getTransactionStatus());
        return null;
    }).when(xaResource).commit(Mockito.any(Xid.class), Mockito.eq(false));
    ctx.finish();
    ArgumentCaptor<Xid> captor = ArgumentCaptor.forClass(Xid.class);
    InOrder inOrder = Mockito.inOrder(xaResource, localResource);
    inOrder.verify(xaResource).start(captor.capture(), Mockito.eq(XAResource.TMNOFLAGS));
    inOrder.verify(xaResource).setTransactionTimeout(Mockito.anyInt());
    inOrder.verify(xaResource).end(Mockito.eq(captor.getValue()), Mockito.eq(XAResource.TMSUCCESS));
    inOrder.verify(xaResource).prepare(captor.getValue());
    inOrder.verify(localResource).commit();
    inOrder.verify(xaResource).commit(Mockito.eq(captor.getValue()), Mockito.eq(false));
    Mockito.verifyNoMoreInteractions(xaResource, localResource);
}
Also used : Xid(javax.transaction.xa.Xid) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 75 with Xid

use of javax.transaction.xa.Xid in project aries by apache.

the class TransactionContextTest method testLastParticipantFailsSoRollback.

@Test
public void testLastParticipantFailsSoRollback() throws Exception {
    ctx.registerLocalResource(localResource);
    ctx.registerXAResource(xaResource, null);
    Mockito.doAnswer(i -> {
        assertEquals(COMMITTING, ctx.getTransactionStatus());
        throw new TransactionException("Unable to commit");
    }).when(localResource).commit();
    Mockito.doAnswer(i -> {
        assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
        return null;
    }).when(xaResource).rollback(Mockito.any(Xid.class));
    ctx.finish();
    ArgumentCaptor<Xid> captor = ArgumentCaptor.forClass(Xid.class);
    InOrder inOrder = Mockito.inOrder(xaResource, localResource);
    inOrder.verify(xaResource).start(captor.capture(), Mockito.eq(XAResource.TMNOFLAGS));
    inOrder.verify(xaResource).setTransactionTimeout(Mockito.anyInt());
    inOrder.verify(xaResource).end(Mockito.eq(captor.getValue()), Mockito.eq(XAResource.TMSUCCESS));
    inOrder.verify(xaResource).prepare(captor.getValue());
    inOrder.verify(localResource).commit();
    inOrder.verify(xaResource).rollback(Mockito.eq(captor.getValue()));
    Mockito.verifyNoMoreInteractions(xaResource, localResource);
}
Also used : Xid(javax.transaction.xa.Xid) TransactionException(org.osgi.service.transaction.control.TransactionException) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Aggregations

Xid (javax.transaction.xa.Xid)82 Test (org.junit.Test)35 XAException (javax.transaction.xa.XAException)20 IOException (java.io.IOException)16 XAResource (javax.transaction.xa.XAResource)14 UnitTest (nl.topicus.jdbc.test.category.UnitTest)11 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)11 LinkedList (java.util.LinkedList)10 InOrder (org.mockito.InOrder)6 HashMap (java.util.HashMap)5 RecoveredXid (nl.topicus.jdbc.xa.RecoveredXid)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)4 ArrayList (java.util.ArrayList)4 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)4 TransactionContext (com.hazelcast.transaction.TransactionContext)3 SerializableXID (com.hazelcast.transaction.impl.xa.SerializableXID)3