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?






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






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)