How should I share connection between Hibernate and other ORM?

In our javaEE application, running on Wildfly 27.0.1.Final with its provided Hibernate 6.1.5, we mainly use our own custom ORM, but we also mix in a bit of JPA / Hibernate.

The goal is that one request should use one connection for everything, no matter what the mixture of our ORM and Hibernate looks like, and commit should only happen once, at the end of the request.

Using connection = sessionImplementor.getJdbcConnectionAccess().obtainConnection() it looks like I get a different connection than what Hibernate uses itself. At least, Hibernate doesn’t see the changes I do on that connection.

Using connection = sessionImplementor.doReturningWork(connection -> connection) seems to work (added it 30 mins ago, so can’t be sure everything works just yet), but it seems really ugly, as if I’m breaking out of a controlled environment.

Is there a “right” way to do this?

Use session.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection()

Thanks. That worked :smile:
This gave the same connection as the doReturningWork “hack”.

Btw, the javadoc on getPhysicalConnection says:
@todo : expose Connection as here? or accept(WorkInConnection) where WorkInConnection is given access to Connection?

So, I don’t feel confident that Hibernate won’t change it again.

If you’re worried, then you will have to implement a custom data source so you are in control of handing connections to Hibernate ORM.