Multi tenancy support in Java EE 6

jakarta-ee, java, java-ee-6, multi-tenant

Solution

Persistence Layer

Start with the persistence layer. Roll upwards through your architecture once you have that done.

The Schema that you are proposing would have an ID that identifies the tenant (eg. TenantId). Each table would have this ID. In all of your queries you would have to ensure that the TenantId matches the logged in User's TenantId.

The difficulty with this is that it is a very manual process.

If you go with Hibernate as your JPA provider then there are some tools that will help with this; namely Hibernate Filters.

These are commonly used to restrict access on multi-tenant Schemas (see here and here for some more)

I haven't used EclipseLink but it does look like it has good support for Multi-Tenancy as well. The DiscriminatorColumn looks like a very similar concept to Hibernate Filters.

Service Layer

I assume that you're using JAX-RS and JMS for a Service Layer. If so then you will also need to think about how you are going to pass the tenantId around and authenticate your Tenants. How are you going to prevent one tenant from accessing the REST service of another? Same thing for JMS.

UI Layer

You are going to have to hook up your login in your UI to a Bean (Hibernate or Eclipselink) that sets the TenantId for the Filter/Discriminator.

Problem

I have an existing Java EE 6 application (deployed in Glassfish v 3.1) and want to support multiple tenants. Technologies/APIs I'm currently using in my app are - EJB (including the EJB timer service) - JPA 2.0 (EclipseLink) - JSF 2.0 - JMS - JAX-RS - I plan to use CDI as well As far as I know, adding multi-tenancy support affects only the persistence layer. My question: Has anybody done this before? What are the steps to convert the application? Will this affect other layers other than persistence? There will be a high number of tenants, therefore, all data will reside in the same DB schema.

Original source

Related problems