When accessing multiple datasources using Spring Boot’s JdbcTemplate, you might encounter the following error:
This error occurs because HikariCP requires specific properties to be set for each datasource.
Environment
Postgresql 10.15+
MySQL 5.7+
Spring Boot 2.3+
JDK 1.8+
Debug
Debug #1: The build.gradle of the app
The app is built using Gradle. Here is the relevant part of the build.gradle file:
Debug #2: The MultipleDBConfig class
To connect to multiple datasources, a @Configuration class is required:
Debug #3: The application.properties file
The connection properties for the datasources are defined in application.properties:
Debug #4: The JDBC client
The JDBC client queries data from the multiple datasources:
Reason
According to the Spring Boot documentation, when using HikariCP, the jdbc-url property must be used instead of url.
Solution
Edit your application.properties to use jdbc-url:
After making this change, re-run the Spring Boot application. The error should be resolved, and the application should connect to both datasources successfully.
All the code is available on GitHub. You can download the example code here.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope: