use of org.osgi.service.transaction.control.TransactionContext in project aries by apache.
the class TransactionControlStatusTest method testInheritRequiredNotSupported.
@Test
public void testInheritRequiredNotSupported() {
txControl.required(() -> {
TransactionContext currentContext = txControl.getCurrentContext();
txControl.notSupported(() -> {
assertNotSame(currentContext, txControl.getCurrentContext());
return null;
});
assertSame(currentContext, txControl.getCurrentContext());
return null;
});
}
use of org.osgi.service.transaction.control.TransactionContext 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;
}
use of org.osgi.service.transaction.control.TransactionContext 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;
}
use of org.osgi.service.transaction.control.TransactionContext in project aries by apache.
the class TransactionControlStatusTest method testInheritRequiredNotSupported.
@Test
public void testInheritRequiredNotSupported() {
txControl.required(() -> {
TransactionContext currentContext = txControl.getCurrentContext();
txControl.notSupported(() -> {
assertNotSame(currentContext, txControl.getCurrentContext());
return null;
});
assertSame(currentContext, txControl.getCurrentContext());
return null;
});
}
Aggregations