1
votes

I'm building an application that should serve multiple tenants and store their very sensitive data. Each tenant has multiple users. My backend stack is built with spring-boot + Hibernate5. I want hibernate to handle multi-tenancy so I have 2 questions for the experts:

  1. Providing I'm using schema-per-tenant paradigm, how do I associate a username to a tenant on login? Each tenant has it's own users table so how do I know to which tenant that username belongs?
  2. Upon registration, what is the proper way to create a new schema on the fly?
2
IMHO Schema per tenant is not best-practice. Instead creation schema using a tenantId column will be more effective. If you have lots of record this will be another issue and can be solved via partitioning, sharding or clustering.Fırat KÜÇÜK

2 Answers

2
votes

If I understand you correctly, I would use the following:

  1. AbstractRoutingDataSource to find/construct a proper datasource
  2. I would basically do the same as with multitenant system. Use Liquibase to handle the schema creation and evolution. But liquibase does not create the target schema, it just creates tables etc. So it would probably require some CREATE SCHEMA through JDBC on customer provisioning.
0
votes

I have implemented a database-per-tenant multitenant web app using Spring Boot 2 and secured user access using Spring Security 5. I am sure you can adapt it to your needs. I have written about how I built it and have also shared the source code. Take a look here.