in tx-control-providers/jpa/tx-control-provider-jpa-xa/src/main/java/org/apache/aries/tx/control/jpa/xa/impl/JPAEntityManagerProviderFactoryImpl.java [558:604]
private Connection enlistedConnection(Callable<Connection> supplier) {
TransactionContext txContext = getTxControl().getCurrentContext();
if (txContext == null) {
throw new TransactionException("The resource " + resourceId
+ " 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 {
toClose = supplier.call();
if (txContext.getTransactionStatus() == NO_TRANSACTION) {
toReturn = new ScopedConnectionWrapper(toClose);
} else if (txContext.supportsXA()) {
toReturn = new TxConnectionWrapper(toClose);
txContext.registerXAResource(getXAResource(toClose), recoveryIdentifier);
} else {
throw new TransactionException(
"There is a transaction active, but it does not support XA 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) {
// TODO log this
}
});
txContext.putScopedValue(resourceId, toReturn);
return toReturn;
}