Connection are obtained via a DataSource that can be configured to support distributed transaction or not. To use multiple connections in a distributed transaction, you must configure the multiple DataSource to support XA and return XA connections.
That said, you need several physical connections only if you connects to different database, which doesn't seem to be your case (that's not clear in the question).
A DataSource can be smart enough to make sure that the same physical connection is used as long as you are in the same thread; each time you ask for a connection, it actually returns a "handle" to the same physical connection, and the physical connection returns to the pool when all handles have been closed. (But that depends on the DataSource implementation).
Hibernate per se is not an XA resource: it uses underlying a connection obtained via a DataSource. But it hooks itself in the transaction manager via JTA, in particular to flush all pending changes before the distributed transaction commits.
You can most of the time obtain the underlying connection used by the EntityManager
using implementation specific API (it's at least possible with Hibernate). This means that you can maybe fulfill your requirement without using JTA and XA at all: use the underlying connection of the EntityManager
for your JDBC stuffs.
In summary:
- No need to mess with XAResource
- Switch to InnoDB
- You can try to switch to a XA DataSource and obtain a connection with
DataSource.getConnection()
- You can try to switch to a XA DataSource and obtain the underlying
EntityManager
connection
- You can try to stick with a non-XA DataSource and obtain the underlying
EntityManager
connection
Hope I understood your question right and that it helps.