Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi tenancy support in Java EE 6

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.

like image 972
Theo Avatar asked Jun 25 '11 14:06

Theo


People also ask

What is multitenant support?

Multi-Tenant – Multi-tenancy means that a single instance of the software and its supporting infrastructure serves multiple customers. Each customer shares the software application and also shares a single database. Each tenant's data is isolated and remains invisible to other tenants.

What is multi-tenancy in Java?

In the realm of enterprise software, especially for software provided as a service, multitenancy ensures that data is truly isolated for each client within a shared instance of software. Among its numerous benefits, multitenancy can greatly simplify release management and cut down costs.

What is multi-tenant with example?

Multitenancy is a software architecture where a single software instance can serve multiple, distinct user groups. Software-as-a-service (SaaS) offerings are an example of multitenant architecture.


1 Answers

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.

like image 86
Damo Avatar answered Oct 11 '22 15:10

Damo