use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.
the class TransactionControlRunningTest method testTwoRequiredsNestedInnerThrowsException.
@Test
public void testTwoRequiredsNestedInnerThrowsException() {
AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
Exception userEx = new Exception("Bang!");
txControl.required(() -> {
assertTrue(txControl.activeTransaction());
Object key = txControl.getCurrentContext().getTransactionKey();
txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
try {
txControl.requiresNew(() -> {
assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
txControl.getCurrentContext().postCompletion(finalStatusInner::set);
txControl.setRollbackOnly();
throw userEx;
});
fail("Should not be reached!");
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
return null;
});
assertEquals(COMMITTED, finalStatusOuter.get());
assertEquals(ROLLED_BACK, finalStatusInner.get());
}
use of org.osgi.service.transaction.control.TransactionStatus in project aries by apache.
the class TransactionControlRunningTest method testTwoRequiredsNestedInnerThrowsExceptionOuterDoesNotCatch.
@Test
public void testTwoRequiredsNestedInnerThrowsExceptionOuterDoesNotCatch() {
AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
Exception userEx = new Exception("Bang!");
try {
txControl.required(() -> {
assertTrue(txControl.activeTransaction());
Object key = txControl.getCurrentContext().getTransactionKey();
txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
txControl.requiresNew(() -> {
assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
txControl.getCurrentContext().postCompletion(finalStatusInner::set);
txControl.setRollbackOnly();
throw userEx;
});
fail("Should not be reached!");
return null;
});
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
assertEquals(ROLLED_BACK, finalStatusOuter.get());
assertEquals(ROLLED_BACK, finalStatusInner.get());
}
use of org.osgi.service.transaction.control.TransactionStatus 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