use of org.osgi.service.transaction.control.TransactionException 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.TransactionException in project aries by apache.
the class TransactionContextImpl method registerXAResource.
@Override
public void registerXAResource(XAResource resource, String name) {
TransactionStatus status = getTransactionStatus();
if (status.compareTo(MARKED_ROLLBACK) > 0) {
throw new IllegalStateException("The current transaction is in state " + status);
}
try {
if (name == null) {
currentTransaction.enlistResource(resource);
} else {
NamedXAResourceImpl res = new NamedXAResourceImpl(name, resource, transactionManager, true);
postCompletion(x -> res.close());
currentTransaction.enlistResource(res);
}
} catch (Exception e) {
throw new TransactionException("The transaction was unable to enlist a resource", e);
}
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class XAJPADataSourceSetup method decorateJPAProperties.
@Override
protected Map<String, Object> decorateJPAProperties(DataSourceFactory dsf, Map<String, Object> providerProperties, Properties jdbcProperties, Map<String, Object> jpaProperties) throws Exception {
DataSource unpooled;
try {
if (toBoolean(providerProperties, USE_DRIVER, false)) {
throw new TransactionException("The Database must use an XA connection");
} else {
unpooled = new XADataSourceMapper(dsf.createXADataSource(jdbcProperties));
}
} catch (SQLException sqle) {
throw new TransactionException("Unable to create the JDBC resource provider", sqle);
}
DataSource toUse = poolIfNecessary(providerProperties, unpooled);
jpaProperties.put("javax.persistence.jtaDataSource", toUse);
return jpaProperties;
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class TransactionContextTest method testMultipleLocalResourcesFirstFailsSoRollback.
@Test
public void testMultipleLocalResourcesFirstFailsSoRollback() throws Exception {
ctx = new TransactionContextImpl(getTxMgr(), true, ENABLED);
ctx.registerLocalResource(localResource);
LocalResource localResource2 = Mockito.mock(LocalResource.class);
ctx.registerLocalResource(localResource2);
Mockito.doAnswer(i -> {
assertEquals(COMMITTING, ctx.getTransactionStatus());
throw new TransactionException("Unable to commit");
}).when(localResource).commit();
Mockito.doAnswer(i -> {
assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
return null;
}).when(localResource2).rollback();
ctx.finish();
Mockito.verify(localResource).commit();
Mockito.verify(localResource2).rollback();
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class TransactionContextTest method testNoLocalResourceCanBeAddedWhenDisabled.
@Test
public void testNoLocalResourceCanBeAddedWhenDisabled() throws Exception {
ctx = new TransactionContextImpl(getTxMgr(), true, DISABLED);
try {
ctx.registerLocalResource(localResource);
fail("A local resource should trigger a failure");
} catch (TransactionException te) {
}
}
Aggregations