10

I am developing a Spring Boot application that uses Spring Data JPA and will need to connect to many different databases e.g. PostreSQL, MySQL, MS-SQL, MongoDB. I need to create all datasources in runtime i.e. user choose these data by GUI in started application: -driver(one of the list), -source, -port, -username, -password. And after all he writes native sql to choosen database and get results. I read a lot of things about it in stack and spring forums(e.g. AbstractRoutingDataSource) but all of these tutorials show how to create datasources from xml configuration or static definition in java bean. It is possible to create many datsources in runtime? How to manage transactions and how to create many sessionFactories? It is possible to use @Transactional annotation? What is the best method to do this? Can someone explain me how to do this 'step by step'?

1
  • Please consider closing your question or marking the answer as the accepted answer if it fits your needs. Commented Oct 7, 2015 at 16:56

1 Answer 1

7

Hope it's not too late for an answer ;)

I developed a module which can be easily integrated in any spring project. It uses a meta-datasource to hold the tenant-datasource connection details. For the tenant-datasource an AbstractRoutingDataSource is used.

Here you find my core implementation using the AbstractRoutingDataSource. https://github.com/Dactabird/multitenancy

Here is an example to show how to integrate it. https://github.com/Dactabird/multitenancy-sample In this example I'm using H2 embedded db. But of course you can use whatever you want.

Feel free to modify it for your purposes or to ask if questions are left!

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

Comments

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.