use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class ConnectionLifecycleTest method testReleaseOfFactoryCreatedService.
@Test
public void testReleaseOfFactoryCreatedService() {
Assume.assumeFalse("Not a factory test", isConfigured());
txControl.required(() -> connection.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )"));
assertEquals("Hello World!", txControl.notSupported(() -> {
ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
return rs.getString(1);
}));
JDBCConnectionProviderFactory factory = (JDBCConnectionProviderFactory) trackers.stream().filter(t -> t.getService() instanceof JDBCConnectionProviderFactory).findFirst().get().getService();
factory.releaseProvider(provider);
try {
assertEquals("Hello World!", txControl.notSupported(() -> {
ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
return rs.getString(1);
}));
fail("Should not be accessible");
} 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 ConnectionLifecycleTest method testDeleteOfConfig.
@Test
public void testDeleteOfConfig() throws Exception {
Assume.assumeTrue("Not a configuration test", isConfigured());
txControl.required(() -> connection.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )"));
assertEquals("Hello World!", txControl.notSupported(() -> {
ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
return rs.getString(1);
}));
ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jdbc.*)");
assertNotNull(configurations);
assertEquals(1, configurations.length);
configurations[0].delete();
Thread.sleep(2000);
try {
assertEquals("Hello World!", txControl.notSupported(() -> {
ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
rs.next();
return rs.getString(1);
}));
fail("Should not be accessible");
} 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 ExceptionManagementTransactionTest method testCheckedException.
@Test
public void testCheckedException() {
URISyntaxException toThrow = new URISyntaxException("yuck", "Bang!");
try {
txControl.required(() -> {
connection.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
throw toThrow;
});
fail("An exception should occur!");
// We have to catch Exception as the compiler complains
// otherwise
} catch (ScopedWorkException swe) {
assertSame(toThrow, swe.getCause());
}
assertRollback();
}
use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class TransactionControlRunningTest method testTwoRequiredsNestedNoRollbackForInnerException.
@Test
public void testTwoRequiredsNestedNoRollbackForInnerException() {
AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
Exception userEx = new BindException("Bang!");
try {
txControl.required(() -> {
assertTrue(txControl.activeTransaction());
Object key = txControl.getCurrentContext().getTransactionKey();
txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
try {
txControl.build().noRollbackFor(BindException.class).requiresNew(() -> {
assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
txControl.getCurrentContext().postCompletion(finalStatusInner::set);
throw userEx;
});
fail("Should not be reached!");
} catch (ScopedWorkException swe) {
throw swe.as(BindException.class);
}
return null;
});
fail("Should not be reached!");
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
assertEquals(ROLLED_BACK, finalStatusOuter.get());
assertEquals(COMMITTED, finalStatusInner.get());
}
use of org.osgi.service.transaction.control.ScopedWorkException in project aries by apache.
the class TransactionControlRunningTest method testRequiredUserException.
@Test
public void testRequiredUserException() {
AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
Exception userEx = new Exception("Bang!");
try {
txControl.required(() -> {
assertTrue(txControl.activeTransaction());
txControl.getCurrentContext().postCompletion(finalStatus::set);
throw userEx;
});
fail("Should not be reached");
} catch (ScopedWorkException swe) {
assertSame(userEx, swe.getCause());
}
assertEquals(ROLLED_BACK, finalStatus.get());
}
Aggregations