use of org.osgi.service.transaction.control.TransactionRolledBackException in project aries by apache.
the class XAJPATransactionTest method testTwoPhaseRollback.
@Test
public void testTwoPhaseRollback() throws Exception {
Object m1 = getMessageEntityFrom(XA_TEST_UNIT_1);
Object m2 = getMessageEntityFrom(XA_TEST_UNIT_2);
Object m3 = getMessageEntityFrom(XA_TEST_UNIT_2);
try {
txControl.required(() -> {
setMessage(m1, "Hello World!");
em1.persist(m1);
setMessage(m2, "Hello 1!");
em2.persist(m2);
txControl.requiresNew(() -> {
setMessage(m3, "Hello 2!");
em2.persist(m3);
return null;
});
txControl.getCurrentContext().registerXAResource(new PoisonResource(), null);
return null;
});
fail("Should roll back");
} catch (TransactionRolledBackException trbe) {
}
assertEquals(0, (int) txControl.notSupported(() -> {
CriteriaBuilder cb = em1.getCriteriaBuilder();
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
countQuery.select(cb.count(countQuery.from(m1.getClass())));
return em1.createQuery(countQuery).getSingleResult().intValue();
}));
assertEquals(Arrays.asList("Hello 2!"), txControl.notSupported(() -> {
CriteriaBuilder cb = em2.getCriteriaBuilder();
CriteriaQuery<String> query = cb.createQuery(String.class);
query.select(query.from(m2.getClass()).get("message"));
return em2.createQuery(query).getResultList();
}));
}
use of org.osgi.service.transaction.control.TransactionRolledBackException in project aries by apache.
the class XATransactionTest method testTwoPhaseRollback.
@Test
public void testTwoPhaseRollback() {
try {
txControl.required(() -> {
connection1.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
connection2.createStatement().execute("Insert into TEST_TABLE values ( 'Hello 1' )");
txControl.requiresNew(() -> {
connection2.createStatement().execute("Insert into TEST_TABLE values ( 'Hello 2' )");
return null;
});
txControl.getCurrentContext().registerXAResource(new PoisonResource(), null);
return null;
});
fail("Should roll back");
} catch (TransactionRolledBackException trbe) {
}
assertEquals(0, (int) txControl.notSupported(() -> {
ResultSet rs = connection1.createStatement().executeQuery("Select count(*) from TEST_TABLE");
rs.next();
return rs.getInt(1);
}));
assertEquals("1: Hello 2", txControl.notSupported(() -> {
Statement s = connection2.createStatement();
ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
rs.next();
int count = rs.getInt(1);
rs = s.executeQuery("Select idValue from TEST_TABLE ORDER BY idValue");
rs.next();
return "" + count + ": " + rs.getString(1);
}));
}
use of org.osgi.service.transaction.control.TransactionRolledBackException 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