Search in sources :

Example 6 with TransactionException

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());
    }
}
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 7 with TransactionException

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);
    }
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) TransactionException(org.osgi.service.transaction.control.TransactionException) SystemException(javax.transaction.SystemException) XAException(javax.transaction.xa.XAException)

Example 8 with TransactionException

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

Example 9 with TransactionException

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();
}
Also used : AbstractTransactionContextImpl(org.apache.aries.tx.control.service.common.impl.AbstractTransactionContextImpl) TransactionException(org.osgi.service.transaction.control.TransactionException) LocalResource(org.osgi.service.transaction.control.LocalResource) Test(org.junit.Test)

Example 10 with TransactionException

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) {
    }
}
Also used : AbstractTransactionContextImpl(org.apache.aries.tx.control.service.common.impl.AbstractTransactionContextImpl) TransactionException(org.osgi.service.transaction.control.TransactionException) 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