use of javax.sql.XAConnection in project aries by apache.
the class Recovery method recover.
public static boolean recover(final String name, final XADataSource dataSource, final RecoverableTransactionManager transactionManager) throws IOException {
if (name != null && name.length() > 0) {
transactionManager.registerNamedXAResourceFactory(new NamedXAResourceFactory() {
public String getName() {
return name;
}
public NamedXAResource getNamedXAResource() throws SystemException {
try {
final XAConnection connection = dataSource.getXAConnection();
LOGGER.debug("new namedXAResource's connection: " + connection);
return new ConnectionAndWrapperNamedXAResource(connection.getXAResource(), getName(), connection);
} catch (Exception e) {
SystemException se = new SystemException("Failed to create ConnectionAndWrapperNamedXAResource, " + e.getLocalizedMessage());
se.initCause(e);
LOGGER.error(se.getLocalizedMessage(), se);
throw se;
}
}
public void returnNamedXAResource(NamedXAResource namedXaResource) {
if (namedXaResource instanceof ConnectionAndWrapperNamedXAResource) {
try {
LOGGER.debug("closing returned namedXAResource's connection: " + ((ConnectionAndWrapperNamedXAResource) namedXaResource).connection);
((ConnectionAndWrapperNamedXAResource) namedXaResource).connection.close();
} catch (Exception ignored) {
LOGGER.debug("failed to close returned namedXAResource: " + namedXaResource, ignored);
}
}
}
});
return true;
} else {
LOGGER.warn("Unable to recover XADataSource: aries.xa.name property not set");
return false;
}
}
use of javax.sql.XAConnection in project aries by apache.
the class XATxContextBindingEntityManagerTest method testActiveTransactionStraightXAConnection.
@Test
public void testActiveTransactionStraightXAConnection() throws SQLException {
Connection con = Mockito.mock(Connection.class, withSettings().extraInterfaces(XAConnection.class));
Mockito.when(((XAConnection) con).getXAResource()).thenReturn(xaResource);
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 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();
}
Aggregations