Modus Operandi
Modus Operandi

Reputation: 641

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:

  1. Since tenant ID changes depending on the request, each request with a new tenant ID needs to manually create a new PersistenceContext (EntityManager) with the appropriate property.
  2. But persistence contexts can't be manually created when using JTA (@PersistenceUnit does not get injected and Persistence.createEntityManagerFactory does not work), according to http://tomee.apache.org/jpa-concepts.html.

Am I missing something? Or is this literally impossible to do?

Upvotes: 0

Views: 231

Answers (1)

John
John

Reputation: 373

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.

Upvotes: 0

Related Questions