AbstractSharedSessionContract.isTransactionCoordinatorShared is never used?!

I’m looking into JTA-coordinated transactions with Hibernate and it seems like it’s not implemented.

In Hibernate 6.5.2, I’m looking at where a org.hibernate.internal.SessionImpl (subclass of org.hibernate.internal.AbstractSharedSessionContract) gets constructed. In the parent AbstractSharedSessionContract constructor, there is logic to determine which type of transaction coordinator to use…JTA vs. JDBC.

To determine this, the isTransactionCoordinatorShared(SessionCreationOptions options) is called. If options is an instance of the org.hibernate.internal.SharedSessionCreationOptions then the transaction coordinator will be JTA. Otherwise, it’ll be JDBC.

The only usage of SharedSessionCreationOptions I see in the Hibernate codebase is in the private inner class SessionImpl.SharedSessionBuilderImpl. The only place that class gets constructed is in the SessionImpl.sessionWithOptions() method. And that sessionWithOptions method is NEVER USED in the Hibernate codebase (beyond delegate wrappers calling it…which will never be populated with a SharedSessionBuilderImpl).

So it appears to me that lines 202-215 of AbstractSharedSessionContract in version 6.5.2 would NEVER be called, and JTA would never coordinate the transactions.

if(isTransactionCoordinatorShare) {

}

What am I not seeing, and how do I ensure that my Hibernate implementation can do the following?

Parent transaction in 1 thread which coordinates calls to 2 separate databases in 2 separate child threads running in parallel?

If JTA is used doesn’t depend on whether isTransactionCoordinatorShare is true. The transactionCoordinator which is built by fastSessionServices.transactionCoordinatorBuilder.buildTransactionCoordinator( jdbcCoordinator, this ) is the thing that controls the transaction. JTA will just work, we have tests for that.

Parent transaction in 1 thread which coordinates calls to 2 separate databases in 2 separate child threads running in parallel?

This is a question that you should ask your JTA implementor. Hibernate just uses either JTA or JDBC resource local transactions. My guess is that you need a 2PC transaction and coordinate that somehow manually, but I have never done that, so hard to say. Maybe it’s easier if you have just 1 thread that manages the transaction and the child threads access the transaction through that thread?

1 Like