12

I am working on a spring boot multi-tenant application with database per tenant strategy. Requirement is that to add new databases at runtime which means I have to create new datasource objects dynamically.

I also looked upon Spring's AbstractRoutingDataSource but in needs pre-defined datasources. So I just want to know how to add/remove datasource without restarting application server.

Thanks in advance.

belnxkkk
  • 375
  • 1
  • 3
  • 11

1 Answers1

13

I finally managed to get things working straight. For someone having same issue, just check this multi-tenant demo project.

belnxkkk
  • 375
  • 1
  • 3
  • 11
  • Your demo needs pre-defined number of datasources. And i want to know how to add/remove datasource without restarting application server , anyone can help me ? Thanks – PhongHv Oct 05 '17 at 10:21
  • Pre-defined datasources are just for demo, it doesn't restrict you to adding new datasources at runtime. Just put a new entry at runtime into datasources map and you are ready to go. – belnxkkk Oct 08 '17 at 08:24
  • @PhongHv we even don't need to put datasources information in external properties files, also possible to put all tenants information in main database and get tenants info from there. HTH – belnxkkk Oct 08 '17 at 08:39
  • We are using sprint boot but do not use JPA features. We query the database by injecting the datasource object into our service and running SQL queries directly using it. Does your solution work in our scenario to make this injected datasource multi tenant? Can we also preserve the transaction management that sprint boot offers? – SRH LABS Apr 17 '18 at 22:38
  • @SRH LABS First of all sorry for late answer, yes this solution works with simply datasource injection and it preserves transaction management. – belnxkkk Mar 07 '19 at 07:02
  • @belnxkkk I was trying the project. When we store call save method from "orderController" how can we redirect the operation to a particular tenant? – Ankit Sharma Apr 03 '20 at 07:01