Can I use two different entity managers that point to the same datasource in one transaction?
-
14-11-2019 - |
문제
I'm using spring with JPA. And I've got two EntityManagers that contain different entities (different modules of one application), and both point to the same datasource.
Can I use both of them in one transaction (using single TransactionManager) ? Do I have to use JTA for that ? If so what is the best option to use it under tomcat ?
How do I configure it in spring ?
해결책
Both standard JPA transactions and Spring's JpaTransactionManager
are bound to a single EntityManager
. They cannot talk to multiple managers, and so cann't coordinate a transaction across them.
If you need to do this, you either need to merge your entity manager configs so you have just one EntityManager
, or use JTA transactions (via Spring's JtaTransactionManager).
If you're using Tomcat (which has no out-of-the-box JTA support), then you'll need to find a third party JTA implementation.