# How should I share connection between Hibernate and other ORM?

**URL:** <https://discourse.hibernate.org/t/how-should-i-share-connection-between-hibernate-and-other-orm/9704>\
**Category:** Hibernate ORM\
**Created:** [June 5, 2024, 1:31pm UTC](https://discourse.hibernate.org/t/how-should-i-share-connection-between-hibernate-and-other-orm/9704 "2024-06-05T13:31:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guttorm](https://avatars.discourse-cdn.com/v4/letter/g/bc79bd/32.png) [@guttorm](https://discourse.hibernate.org/u/guttorm)\
**Post date:** [June 5, 2024, 1:31pm UTC](https://discourse.hibernate.org/t/how-should-i-share-connection-between-hibernate-and-other-orm/9704/1 "2024-06-05T13:31:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 5, 2024, 1:59pm UTC](https://discourse.hibernate.org/t/how-should-i-share-connection-between-hibernate-and-other-orm/9704/2 "2024-06-05T13:59:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guttorm](https://avatars.discourse-cdn.com/v4/letter/g/bc79bd/32.png) [@guttorm](https://discourse.hibernate.org/u/guttorm)\
**Post date:** [June 6, 2024, 8:27am UTC](https://discourse.hibernate.org/t/how-should-i-share-connection-between-hibernate-and-other-orm/9704/3 "2024-06-06T08:27:28Z")

</div>

> [@beikov](#):
>
> session.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection()

Thanks. That worked 😄  
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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 6, 2024, 5:06pm UTC](https://discourse.hibernate.org/t/how-should-i-share-connection-between-hibernate-and-other-orm/9704/4 "2024-06-06T17:06:10Z")

</div>

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.
