use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class ConnectionLifecycleTest method testUpdateOfConfig.
@Test
public void testUpdateOfConfig() 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].update();
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 XAEnabledTxContextBindingConnection method getDelegate.
@Override
protected final Connection getDelegate() {
TransactionContext txContext = txControl.getCurrentContext();
if (txContext == null) {
throw new TransactionException("The resource " + provider + " cannot be accessed outside of an active Transaction Context");
}
Connection existing = (Connection) txContext.getScopedValue(resourceId);
if (existing != null) {
return existing;
}
Connection toReturn;
Connection toClose;
try {
if (txContext.getTransactionStatus() == NO_TRANSACTION) {
toClose = provider.getConnection();
toReturn = new ScopedConnectionWrapper(toClose);
} else if (txContext.supportsXA() && xaEnabled) {
toClose = provider.getConnection();
toReturn = new TxConnectionWrapper(toClose);
txContext.registerXAResource(getXAResource(toClose), recoveryIdentifier);
} else if (txContext.supportsLocal() && localEnabled) {
toClose = provider.getConnection();
toReturn = new TxConnectionWrapper(toClose);
txContext.registerLocalResource(getLocalResource(toClose));
} else {
throw new TransactionException("There is a transaction active, but it does not support local participants");
}
} catch (Exception sqle) {
throw new TransactionException("There was a problem getting hold of a database connection", sqle);
}
txContext.postCompletion(x -> {
try {
toClose.close();
} catch (SQLException sqle) {
}
});
txContext.putScopedValue(resourceId, toReturn);
return toReturn;
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class JDBCConnectionProviderFactoryImpl method getProviderFor.
@Override
public AbstractJDBCConnectionProvider getProviderFor(XADataSource ds, Map<String, Object> resourceProviderProperties) {
checkEnlistment(resourceProviderProperties);
DataSource unpooled;
if (ds instanceof DataSource) {
unpooled = (DataSource) ds;
} else {
throw new TransactionException("This resource Provider does not support XA transactions, and the supplied XADataSource is not a DataSource");
}
return new JDBCConnectionProviderImpl(poolIfNecessary(resourceProviderProperties, unpooled));
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class JDBCConnectionProviderFactoryImpl method getProviderFor.
@Override
public AbstractJDBCConnectionProvider getProviderFor(DataSourceFactory dsf, Properties jdbcProperties, Map<String, Object> resourceProviderProperties) {
checkEnlistment(resourceProviderProperties);
DataSource unpooled;
try {
if (toBoolean(resourceProviderProperties, USE_DRIVER, false)) {
unpooled = new DriverDataSource(dsf.createDriver(null), jdbcProperties.getProperty(JDBC_URL), jdbcProperties);
} else {
unpooled = dsf.createDataSource(jdbcProperties);
}
} catch (SQLException sqle) {
throw new TransactionException("Unable to create the JDBC resource provider", sqle);
}
DataSource toUse = poolIfNecessary(resourceProviderProperties, unpooled);
return new JDBCConnectionProviderImpl(toUse);
}
use of org.osgi.service.transaction.control.TransactionException 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());
}
}
Aggregations