Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Multi-Tenancy - Adding new tenant without restart (using AbstractRoutingDataSource)

I'm implementing a multi-tenant system using Spring where each tenant has its own database. I have everything up and running.

I've extended "AbstractRoutingDataSource" and overridden "determineCurrentLookupKey" to determine which connection to use via the users domain/tenancy.

The "AbstractRoutingDataSource" is instantiated when the app loads and all the possible database connections are set there.

Here's my question -

Is there a way of dynamically adding additional connections to the AbstractRoutingDataSource? I want to be able to add additional tenants without restarting.

Any help would be greatly appreciated

Thanks

like image 841
Jon C Avatar asked Mar 13 '23 16:03

Jon C


2 Answers

I am too late to this thread, but recently I needed to build a Saas style multi-tenant web app. Tenants needed to be added dynamically without requiring a server restart. So I shared my learnings here with a complete working example.

like image 124
sunitkatkar Avatar answered Mar 15 '23 06:03

sunitkatkar


Yes You can do So, Here is complete project which explains very nicely about your requirement: Multi-tenancy: Managing multiple datasources with Spring Data JPA

like image 22
Ritesh Singh Avatar answered Mar 15 '23 06:03

Ritesh Singh