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.

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

6 Comments

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
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.
@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
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 First of all sorry for late answer, yes this solution works with simply datasource injection and it preserves transaction management.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.