Search in sources :

Example 16 with TransactionException

use of org.osgi.service.transaction.control.TransactionException in project aries by apache.

the class JPALifecycleTest method testUpdateOfConfig.

@Test
public void testUpdateOfConfig() 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].update();
    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)

Example 17 with TransactionException

use of org.osgi.service.transaction.control.TransactionException in project aries by apache.

the class JPALifecycleTest method doBundleStoppingTest.

private void doBundleStoppingTest(Predicate<Bundle> p, String exceptionMessage) {
    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));
    List<Bundle> toStop = Arrays.stream(context.getBundles()).filter(p).collect(toList());
    System.out.println(toStop);
    try {
        toStop.stream().forEach(b -> {
            System.out.println("Stopping " + b.getSymbolicName());
            try {
                b.stop();
            } catch (BundleException e) {
            }
        });
        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(exceptionMessage, swe.getCause().getMessage());
        } catch (TransactionException te) {
            assertEquals(exceptionMessage, te.getMessage());
        }
    } finally {
        toStop.stream().forEach(b -> {
            System.out.println("Restarting " + b.getSymbolicName());
            try {
                b.start();
            } catch (BundleException e) {
            }
        });
        getService(JPAEntityManagerProvider.class, 5000);
    }
}
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) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) BundleException(org.osgi.framework.BundleException)

Example 18 with TransactionException

use of org.osgi.service.transaction.control.TransactionException in project aries by apache.

the class LocalJPADataSourceSetup method decorateJPAProperties.

@Override
protected Map<String, Object> decorateJPAProperties(DataSourceFactory dsf, Map<String, Object> providerProperties, Properties jdbcProperties, Map<String, Object> jpaProperties) {
    DataSource unpooled;
    try {
        if (toBoolean(providerProperties, 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(providerProperties, unpooled);
    jpaProperties.put(JAVAX_PERSISTENCE_NON_JTA_DATA_SOURCE, toUse);
    return jpaProperties;
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) SQLException(java.sql.SQLException) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource)

Example 19 with TransactionException

use of org.osgi.service.transaction.control.TransactionException in project aries by apache.

the class TransactionContextTest method testLocalResourcesFirstFailsSoRollback.

@Test
public void testLocalResourcesFirstFailsSoRollback() throws Exception {
    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 : TransactionException(org.osgi.service.transaction.control.TransactionException) LocalResource(org.osgi.service.transaction.control.LocalResource) Test(org.junit.Test)

Example 20 with TransactionException

use of org.osgi.service.transaction.control.TransactionException in project aries by apache.

the class XATxContextBindingEntityManager 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;
    }
    TransactionControl previous = commonTxStore.get();
    commonTxStore.set(txControl);
    EntityManager toReturn;
    EntityManager toClose;
    try {
        if (txContext.getTransactionStatus() == NO_TRANSACTION) {
            toClose = provider.createEntityManager();
            toReturn = new ScopedEntityManagerWrapper(toClose);
        } else if (txContext.supportsXA()) {
            toClose = provider.createEntityManager();
            toReturn = new TxEntityManagerWrapper(toClose);
            toClose.joinTransaction();
        } else {
            throw new TransactionException("There is a transaction active, but it does not support local participants");
        }
    } catch (Exception sqle) {
        commonTxStore.set(previous);
        throw new TransactionException("There was a problem getting hold of a database connection", sqle);
    }
    txContext.postCompletion(x -> {
        try {
            toClose.close();
        } catch (PersistenceException sqle) {
        }
        commonTxStore.set(previous);
    });
    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) TransactionControl(org.osgi.service.transaction.control.TransactionControl) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException) TransactionException(org.osgi.service.transaction.control.TransactionException)

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