use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class ExceptionManagementTransactionTest method testNoRollbackForException.
@Test
public void testNoRollbackForException() {
RuntimeException toThrow = new RuntimeException("Bang!");
try {
txControl.build().noRollbackFor(RuntimeException.class).required(() -> {
PreparedStatement ps = connection.prepareStatement("Insert into TEST_TABLE values ( ? )");
ps.setString(1, "Hello World!");
ps.executeUpdate();
throw toThrow;
});
fail("An exception should occur!");
// We have to catch Exception as the compiler complains
// otherwise
} catch (ScopedWorkException swe) {
assertSame(toThrow, swe.getCause());
}
assertEquals("1: Hello World!", txControl.notSupported(() -> {
Statement s = connection.createStatement();
ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
rs.next();
int count = rs.getInt(1);
rs = s.executeQuery("Select message from TEST_TABLE ORDER BY message");
rs.next();
return "" + count + ": " + rs.getString(1);
}));
}
use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class JPALifecycleTest method testDeleteOfConfig.
@Test
public void testDeleteOfConfig() throws Exception {
Message m = new Message();
m.message = "Hello World";
txControl.required(() -> {
em.persist(m);
return null;
});
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jpa.*)");
assertNotNull(configurations);
assertEquals(1, configurations.length);
configurations[0].delete();
Thread.sleep(2000);
try {
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
} catch (ScopedWorkException swe) {
assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
}
}
use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class JPALifecycleTest method testUpdateOfConfig.
@Test
public void testUpdateOfConfig() throws Exception {
Message m = new Message();
m.message = "Hello World";
txControl.required(() -> {
em.persist(m);
return null;
});
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jpa.*)");
assertNotNull(configurations);
assertEquals(1, configurations.length);
configurations[0].update();
Thread.sleep(2000);
try {
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
} catch (ScopedWorkException swe) {
assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
}
}
use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class JPALifecycleTest method doBundleStoppingTest.
private void doBundleStoppingTest(Predicate<Bundle> p, String exceptionMessage) {
Message m = new Message();
m.message = "Hello World";
txControl.required(() -> {
em.persist(m);
return null;
});
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
List<Bundle> toStop = Arrays.stream(context.getBundles()).filter(p).collect(toList());
System.out.println(toStop);
try {
toStop.stream().forEach(b -> {
System.out.println("Stopping " + b.getSymbolicName());
try {
b.stop();
} catch (BundleException e) {
}
});
try {
assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
} catch (ScopedWorkException swe) {
assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
assertEquals(exceptionMessage, swe.getCause().getMessage());
} catch (TransactionException te) {
assertEquals(exceptionMessage, te.getMessage());
}
} finally {
toStop.stream().forEach(b -> {
System.out.println("Restarting " + b.getSymbolicName());
try {
b.start();
} catch (BundleException e) {
}
});
getService(JPAEntityManagerProvider.class, 5000);
}
}
use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class TransactionControlRunningTest method testTwoRequiredsNestedOuterThrowsException.
@Test
public void testTwoRequiredsNestedOuterThrowsException() {
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.setRollbackOnly();
txControl.requiresNew(() -> {
assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
txControl.getCurrentContext().postCompletion(finalStatusInner::set);
return null;
});
throw userEx;
});
fail("Should not be reached");
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
assertEquals(ROLLED_BACK, finalStatusOuter.get());
assertEquals(COMMITTED, finalStatusInner.get());
}
Aggregations