use of javax.sql.XAConnection in project aries by apache.
the class XATxContextBindingEntityManagerTest method testActiveTransactionWrappedXAConnection.
@Test
public void testActiveTransactionWrappedXAConnection() throws SQLException {
XAConnection con = Mockito.mock(XAConnection.class);
Connection raw = Mockito.mock(Connection.class);
Mockito.when(con.getXAResource()).thenReturn(xaResource);
Mockito.when(con.getConnection()).thenReturn(raw);
XAConnectionWrapper value = new XAConnectionWrapper(con);
Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(value);
setupActiveTransaction();
em.isOpen();
em.isOpen();
Mockito.verify(rawEm, times(2)).isOpen();
Mockito.verify(rawEm).joinTransaction();
checkPostCompletion(null);
}
use of javax.sql.XAConnection in project aries by apache.
the class XATxContextBindingEntityManagerTest method testActiveTransactionUnwrappableXAConnectionWrapper.
@Test
public void testActiveTransactionUnwrappableXAConnectionWrapper() throws SQLException {
XAConnection xaCon = Mockito.mock(XAConnection.class);
Mockito.when(xaCon.getXAResource()).thenReturn(xaResource);
Connection con = Mockito.mock(Connection.class);
XAConnectionWrapper toReturn = new XAConnectionWrapper(xaCon);
Mockito.when(con.unwrap(XAConnectionWrapper.class)).thenReturn(toReturn);
Mockito.when(con.isWrapperFor(XAConnectionWrapper.class)).thenReturn(true);
Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(con);
setupActiveTransaction();
em.isOpen();
em.isOpen();
Mockito.verify(rawEm, times(2)).isOpen();
Mockito.verify(rawEm).joinTransaction();
checkPostCompletion(null);
}
use of javax.sql.XAConnection 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();
}
use of javax.sql.XAConnection in project aries by apache.
the class TransactionLogTest method testRequiredRecoverable.
@Test
public void testRequiredRecoverable() throws Exception {
XAConnection xaConn = dataSource.getXAConnection();
try {
txControl.required(() -> {
txControl.getCurrentContext().registerXAResource(xaConn.getXAResource(), "foo");
Connection conn = xaConn.getConnection();
return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
});
} finally {
xaConn.close();
}
try (Connection conn = dataSource.getConnection()) {
ResultSet rs = conn.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
assertEquals("Hello World!", rs.getString(1));
}
}
use of javax.sql.XAConnection in project aries by apache.
the class TransactionLogTest method testRequiredRecoveryRequiredPrePrepare.
@Test
public void testRequiredRecoveryRequiredPrePrepare() throws Exception {
doRecoveryRequired((good, poison) -> {
txControl.getCurrentContext().registerXAResource(poison, null);
txControl.getCurrentContext().registerXAResource(good, "foo");
}, TransactionStatus.ROLLED_BACK);
boolean success = false;
XAConnection conn = dataSource.getXAConnection();
for (int i = 0; i < 5; i++) {
if (conn.getXAResource().recover(XAResource.TMSTARTRSCAN).length == 0) {
success = true;
break;
} else {
// Wait for recovery to happen!
Thread.sleep(500);
}
}
assertTrue("No recovery in time", success);
}
Aggregations