spring add datasource at runtime
Asked Answered
P

1

13

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.

Porfirioporgy answered 1/10, 2017 at 18:4 Comment(0)
P
14

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

Porfirioporgy answered 3/10, 2017 at 17:17 Comment(6)
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 ? ThanksMulholland
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.Porfirioporgy
@Mulholland 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. HTHPorfirioporgy
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?Mentality
@SRH LABS First of all sorry for late answer, yes this solution works with simply datasource injection and it preserves transaction management.Porfirioporgy
@Porfirioporgy I was trying the project. When we store call save method from "orderController" how can we redirect the operation to a particular tenant?Eiland

© 2022 - 2024 — McMap. All rights reserved.