12

According to this article, you can use the AbstractRoutingDataSource from Spring Framework to dynamically change the data source used by the application.

However, the data sources used are defined by configuration, instead of programmatically. Is there a way of configuring the data sources to be used at runtime?

How scalable is this solution, i.e., what are the limitations in number of data sources?

Thanks!

Carlos Melo
  • 3,052
  • 3
  • 37
  • 45

1 Answers1

8

I have implemented this approach for 30 datasources and they are currently running in a Production environment without any issues. If you are using JPA you can have a look at my config:

dynamic-datasource-routing

Community
  • 1
  • 1
Michael W
  • 3,515
  • 8
  • 39
  • 62
  • 1
    Is there any documentation for to use AbstractRoutingDataSource in spring boot framework?. If yes , can you please share the documentation or any tutorial? , I am using spring boot for developing microservices. also spring data JPA . – Mr.DevEng Mar 06 '18 at 14:25
  • Same as @Jacob, I need to be linked with 6 datasources. – cactuschibre Jun 25 '18 at 16:18
  • See https://stackoverflow.com/questions/48674412/multiple-data-sources-with-same-entity-and-repo/48675156#48675156. And resources : http://tech.asimio.net/2017/01/17/Multitenant-applications-using-Spring-Boot-JPA-Hibernate-and-Postgres.html and https://rjbtechnology.com/blog/2017-01/discriminator-multitenancy-with-spring-and-hibernate-no-filters/. – cactuschibre Jun 27 '18 at 07:58