Description
A surprising amount of work--entirely boilerplate--is currently necessary for a Spring Boot app to utilize two (or more) Datasources with JPA. There is fantastic support for a single implicit Datasource (and autowiring it for JPA use) by configuring a handful of properties. By comparison an extraordinary amount of work--annotations and interfaces/classes--are requried to support a second Datasource with JPA (EnableTransactionManagement
, EnableJpaRepositories
, EntityManagerFactory
, EntityManagerFactoryBuilder
, PlatformTransactionManager
, JpaTransactionManager
, DataSourceBuilder
, LocalContainerEntityManagerFactoryBean
, ...)
This issue proposes that Spring Boot configuration guidelines--and supporting changes to autowiring--be developed such that adding additional Datasources for JPA doesn't require departing from a configuration-over-code mindset. The current approach certainly falls afoul of the Principle of Least Astonishment:
In general engineering design contexts, the principle can be taken to mean that a component of a system should behave in a manner consistent with how users of that component are likely to expect it to behave; that is, users should not be astonished at the way it behaves
The fact that so many developers spend time troubleshooting and discussing how to accomplish this is good evidence that a simplification is in order.
See also:
- https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7
- https://stackoverflow.com/questions/27614301/spring-boot-multiple-datasource
- http://www.onlinetutorialspoint.com/spring-boot/spring-boot-multiple-data-sources-example.html
- http://roufid.com/spring-boot-multiple-databases-configuration/
- https://scattercode.co.uk/2016/01/05/multiple-databases-with-spring-boot-and-spring-data-jpa/
- http://www.baeldung.com/spring-data-jpa-multiple-databases
- http://www.javaoptimum.com/how-to-configure-multiple-datasources-with-spring-boot/
- ...
- Issue Improve documentation showing how to use two different databases #3456
- Issue Provide an example of how to configure multiple datasources and configuration consumption #7652