Search in sources :

Example 1 with TransactionException

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;
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) SQLException(java.sql.SQLException) TransactionContext(org.osgi.service.transaction.control.TransactionContext) TxConnectionWrapper(org.apache.aries.tx.control.jdbc.common.impl.TxConnectionWrapper) Connection(java.sql.Connection) ScopedConnectionWrapper(org.apache.aries.tx.control.jdbc.common.impl.ScopedConnectionWrapper) SQLException(java.sql.SQLException) TransactionException(org.osgi.service.transaction.control.TransactionException)

Example 2 with TransactionException

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);
    }
}
Also used : XADataSource(javax.sql.XADataSource) TransactionException(org.osgi.service.transaction.control.TransactionException) SQLException(java.sql.SQLException) XADataSourceMapper(org.apache.aries.tx.control.jdbc.xa.connection.impl.XADataSourceMapper) XADataSource(javax.sql.XADataSource) DriverDataSource(org.apache.aries.tx.control.jdbc.common.impl.DriverDataSource) DataSource(javax.sql.DataSource)

Example 3 with TransactionException

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));
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) DriverDataSource(org.apache.aries.tx.control.jdbc.common.impl.DriverDataSource) SQLException(java.sql.SQLException) XADataSourceMapper(org.apache.aries.tx.control.jdbc.xa.connection.impl.XADataSourceMapper) XADataSource(javax.sql.XADataSource) DriverDataSource(org.apache.aries.tx.control.jdbc.common.impl.DriverDataSource) DataSource(javax.sql.DataSource)

Example 4 with TransactionException

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;
}
Also used : EntityManager(javax.persistence.EntityManager) ScopedEntityManagerWrapper(org.apache.aries.tx.control.jpa.common.impl.ScopedEntityManagerWrapper) TransactionException(org.osgi.service.transaction.control.TransactionException) TxEntityManagerWrapper(org.apache.aries.tx.control.jpa.common.impl.TxEntityManagerWrapper) TransactionContext(org.osgi.service.transaction.control.TransactionContext) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException) TransactionException(org.osgi.service.transaction.control.TransactionException)

Example 5 with TransactionException

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());
    }
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionException(org.osgi.service.transaction.control.TransactionException) JDBCConnectionProviderFactory(org.osgi.service.transaction.control.jdbc.JDBCConnectionProviderFactory) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

TransactionException (org.osgi.service.transaction.control.TransactionException)23 Test (org.junit.Test)10 SQLException (java.sql.SQLException)8 DataSource (javax.sql.DataSource)6 ScopedWorkException (org.osgi.service.transaction.control.ScopedWorkException)6 XADataSource (javax.sql.XADataSource)4 DriverDataSource (org.apache.aries.tx.control.jdbc.common.impl.DriverDataSource)4 Configuration (org.osgi.service.cm.Configuration)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 TransactionContext (org.osgi.service.transaction.control.TransactionContext)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 Message (org.apache.aries.tx.control.itests.entity.Message)3 XADataSourceMapper (org.apache.aries.tx.control.jdbc.xa.connection.impl.XADataSourceMapper)3 LocalResource (org.osgi.service.transaction.control.LocalResource)3 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 EntityManager (javax.persistence.EntityManager)2 PersistenceException (javax.persistence.PersistenceException)2 ScopedConnectionWrapper (org.apache.aries.tx.control.jdbc.common.impl.ScopedConnectionWrapper)2 TxConnectionWrapper (org.apache.aries.tx.control.jdbc.common.impl.TxConnectionWrapper)2