EclipseLink (JPA) table-based multitenancy with JTA, how?
EclipseLink (JPA) table-based multitenancy with JTA, how?
Our application project is an OSGI bundle using JPA with EclipseLink and JTA, and needs single-table multi-tenancy, where tenant ID comes from a REST request. From what I've read and tried, it almost seems that is impossible:
PersistenceContext
EntityManager
@PersistenceUnit
Persistence.createEntityManagerFactory
Am I missing something? Or is this literally impossible to do?
I'm not sure wether it's EE or not, we're using ServiceMix as the platform (Apache Karaf OSGI) and EclipseLink as the JPA implementation for persistence. Blueprint is the dependency manager tying it all together.
– Modus Operandi
Sep 11 '18 at 11:41
So try adding a @PersistenceUnit to get an EMF and obtain the EntityManager from it yourself. Calling em.flush somewhere in your transactional methods should throw an exception right away if it isn't hooked into JTA correctly. You may have to call em.joinTransaction.
– Chris
Sep 11 '18 at 13:59
@PersistenceUnit
annotated field does not get injected (is always null
) when using JTA.– Modus Operandi
Sep 11 '18 at 17:04
@PersistenceUnit
null
1 Answer
1
You can set multitenant/discriminator properties in the entity manager for a request. But it is not safe for multi-threading and lazy initialization.
I tried our CMobileCom JPA that supports single-table multitenancy. For each tenant, a new EntityManager should be used. That is, an EntityManager should not be shared to access data for multiple tenants. This is also true for EclipseLink.
Disclaimer: I am a developer of CMobileCom JPA, a light weight JPA implementation for Java and Android.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The doc you state is TomEE specific - most other containers allow you to get the EntityManagerFactory in a JTA environment with EntityManagers participating in the JTA transaction. Is TomEE disallowing the @PersistenceUnit with transaction-type="JTA" ?
– Chris
Sep 10 '18 at 15:53