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