All Projects → rajendrag → play-jpa-multitenant

rajendrag / play-jpa-multitenant

Licence: other
Multi tenancy with Play Framework 2 + JPA (Hibernate) : Schema per tenant

Programming Languages

java
68154 projects - #9 most used programming language
scala
5932 projects
TSQL
950 projects
HTML
75241 projects

Play-JPA-Multitenant

A very simple PlayFramework application, to demonstrate how to build a multi tenant (schema/db per tenant) application using JPA(Hibernate).

Assumptions:

  • There will be a common database which will have the "common to all tenants" information. It also has a table com_tenant which will provide information about all the tenants.
  • There will be one separate database/schema per tenant (All these databases will have exact same structure like same table names and column names etc)
  • Uses Hibernate as the JPA provider.
  • Using Hibernate dynamic data source routing.
  • tenant id will be available in Http.Context.
  • Common DB evolutions are inside evolutions/commondb and tenant databases evolutions are inside evolutions/application.

It also handles the db migrations (evolutions) properly. The scripts in evolutions/application will be applied on all the tenant databases

Important Classes

  • IRequestHandler Play's RequestHandler's implementation, Decides and sets the current tenant for that request in Http.Context
  • TenantIdResolver Provides the tenant id which will be used to switch to the proper data source
  • MultiTenantConnectionProviderImpl Hibernates's ConnecitonProvider, switches the DataSource dynamically based on the tenant id provided by the resolver.
  • MultiTenantEvolutionsExecutor Addon to Play's evolutions module to execute scripts from evolutions/appliction on to all tenant databases.

Install Activator

Install latest version of activator from https://www.typesafe.com/activator/download unzip and add Activator to your PATH to have the command available in your terminal.

Create DBs

Execute sql/create_database.sql in mysql.

Setup

git clone https://github.com/rajendrag/play-jpa-multitenant.git
cd play-jpa-multitenant
activator "run" (or to debug activator -jvm-debug 9999 run)

This will start the application on port 9000 in auto reload mode, so that you can see your changes instantly

Testing

http://localhost:9000/api/units
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].