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