I have a Spring Boot App which is currently connected to a single database using JPA. Connection details in its application.properties file:
spring.datasource.url=jdbc:oracle:thin:@localhost:1522:orcl
spring.datasource.username=example
spring.datasource.password=example
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
Now I have a new database I would like to connect to, so my application connects with 2 databases now. I can connect it using JDBC, there I will have to manually write connection code.
I have looked for solutions. Some are in hibernate with multiple config files. Others are using JDBC.
I tried looking in spring docs if there are provisions for defining multiple datasources in application.properties file, but couldn't find anything.
I would like to know if a solution exists using JPA alone. Thanks for your time.
Follow the below steps:
Add an additional datasource configuration to your application.properties.
Set the SQL Dialect to “default” in your application.properties to let Spring autodetect the different SQL Dialects of each datasource
Full code is available here,
https://github.com/jahe/spring-boot-multiple-datasources
Above steps are took from this tutorials
https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7
Hope this will helps:)
Create Different DataSource bean in your configuration file. You should able to provide all db related configuration in beans:
@Bean
public DataSource db1(String url, String dbuser, String password) {
}
@Bean
public DataSource db2(String url, String dbuser, String password) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With