use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class TransactionContextTest method testSecondLocalResourceCannotBeAdded.
@Test
public void testSecondLocalResourceCannotBeAdded() throws Exception {
ctx.registerLocalResource(localResource);
LocalResource localResource2 = Mockito.mock(LocalResource.class);
try {
ctx.registerLocalResource(localResource2);
fail("A second local resource should trigger a failure");
} catch (TransactionException te) {
}
}
use of org.osgi.service.transaction.control.TransactionException 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);
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class TransactionLogTest method doRecoveryRequired.
public void doRecoveryRequired(BiConsumer<XAResource, XAResource> ordering, TransactionStatus expectedFinalState) throws Exception {
//Register the recoverable resource
ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
Mockito.verify(ctx).addServiceListener(captor.capture(), Mockito.anyString());
Mockito.when(ctx.getService(serviceRef)).thenReturn(new TestRecoverableResource("foo", dataSource));
captor.getValue().serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
XAConnection xaConn = dataSource.getXAConnection();
AtomicReference<TransactionStatus> ref = new AtomicReference<TransactionStatus>();
try {
txControl.required(() -> {
txControl.getCurrentContext().postCompletion(ref::set);
Connection conn = xaConn.getConnection();
// conn.setAutoCommit(false);
XAResource dsResource = xaConn.getXAResource();
XAResource poison = Mockito.mock(XAResource.class);
Mockito.when(poison.prepare(Mockito.any())).thenAnswer(i -> {
conn.createStatement().execute("shutdown immediately");
Thread.sleep(1000);
return XA_OK;
});
ordering.accept(dsResource, poison);
return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
});
} catch (TransactionException te) {
assertEquals(expectedFinalState, ref.get());
assertEquals(expectedFinalState == ROLLED_BACK, te instanceof TransactionRolledBackException);
} finally {
try {
xaConn.close();
} catch (SQLException sqle) {
}
}
setupServerAndDataSource();
}
Aggregations