use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class TxContextBindingConnection 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.supportsLocal()) {
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(DataSource ds, Map<String, Object> resourceProviderProperties) {
boolean xaEnabled = toBoolean(resourceProviderProperties, XA_ENLISTMENT_ENABLED, true);
boolean localEnabled = toBoolean(resourceProviderProperties, LOCAL_ENLISTMENT_ENABLED, true);
try {
checkEnlistment(xaEnabled, localEnabled, ds.isWrapperFor(XADataSource.class));
DataSource toUse = poolIfNecessary(resourceProviderProperties, xaEnabled ? new XADataSourceMapper(ds.unwrap(XADataSource.class)) : ds);
return new JDBCConnectionProviderImpl(toUse, xaEnabled, localEnabled, getRecoveryId(resourceProviderProperties, xaEnabled));
} catch (SQLException sqle) {
throw new TransactionException("Unable to create the JDBC resource provider", sqle);
}
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class JDBCConnectionProviderFactoryImpl method getProviderFor.
@Override
public JDBCConnectionProviderImpl getProviderFor(DataSourceFactory dsf, Properties jdbcProperties, Map<String, Object> resourceProviderProperties) {
boolean xaEnabled = toBoolean(resourceProviderProperties, XA_ENLISTMENT_ENABLED, true);
boolean localEnabled = toBoolean(resourceProviderProperties, LOCAL_ENLISTMENT_ENABLED, true);
boolean useDriver = toBoolean(resourceProviderProperties, USE_DRIVER, false);
checkEnlistment(xaEnabled, localEnabled, !useDriver);
DataSource unpooled;
try {
if (useDriver) {
unpooled = new DriverDataSource(dsf.createDriver(null), jdbcProperties.getProperty(JDBC_URL), jdbcProperties);
} else if (xaEnabled) {
unpooled = new XADataSourceMapper(dsf.createXADataSource(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, xaEnabled, localEnabled, getRecoveryId(resourceProviderProperties, xaEnabled));
}
use of org.osgi.service.transaction.control.TransactionException in project aries by apache.
the class TxContextBindingEntityManager method getRealEntityManager.
@Override
protected final EntityManager getRealEntityManager() {
TransactionContext txContext = txControl.getCurrentContext();
if (txContext == null) {
throw new TransactionException("The resource " + provider + " cannot be accessed outside of an active Transaction Context");
}
EntityManager existing = (EntityManager) txContext.getScopedValue(resourceId);
if (existing != null) {
return existing;
}
EntityManager toReturn;
EntityManager toClose;
try {
if (txContext.getTransactionStatus() == NO_TRANSACTION) {
toClose = provider.createEntityManager();
toReturn = new ScopedEntityManagerWrapper(toClose);
} else if (txContext.supportsLocal()) {
toClose = provider.createEntityManager();
toReturn = new TxEntityManagerWrapper(toClose);
txContext.registerLocalResource(getLocalResource(toClose));
toClose.getTransaction().begin();
} 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 (PersistenceException sqle) {
}
});
txContext.putScopedValue(resourceId, toReturn);
return toReturn;
}
use of org.osgi.service.transaction.control.TransactionException 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());
}
}
Aggregations