Search in sources :

Example 11 with TransactionException

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

Example 12 with TransactionException

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;
}
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) TransactionException(org.osgi.service.transaction.control.TransactionException) SQLException(java.sql.SQLException)

Example 13 with TransactionException

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

Example 14 with TransactionException

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

Example 15 with TransactionException

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());
    }
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionException(org.osgi.service.transaction.control.TransactionException) Message(org.apache.aries.tx.control.itests.entity.Message) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) 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