4
votes

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

2
Not with the AbstractRoutingDataSource. We used/developed this about 10 years ago, still works like charm in production. It is actually more flexible then just a datasource (we used it for messaging, theming, properties etc.).M. Deinum
Thanks @M.Deinum it was a lot more straight forward than I thought. The AbstractRoutingDataSource class threw me a curve ball. Following your example I've managed to get things up and running using the AbstractDataSource class instead.Thanks again!Jon C

2 Answers

4
votes

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.

2
votes

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