I want to connect two different MySQL databases with spring boot application. I want application to be able to communicate with both databases at the same time.
Asked
Active
Viewed 474 times
0
-
Just have two datasources and communicate with the correct database when you need to interact with it? There are numerous articles about this subject. Please do some research first. That is a part of your job. – KarelG May 30 '18 at 06:34
-
Possible duplicate of: https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources – damjad May 30 '18 at 06:35
1 Answers
1
You have to decleare different keys for different datasource.
@Bean
@Primary
@ConfigurationProperties("mysql.connection1")
public DataSourceProperties connectionProp1() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("mysql.connection1")
public DataSource connection1() {
return connectionProp1().initializeDataSourceBuilder().build();
}
@Bean
@Primary
@ConfigurationProperties("mysql.connection2")
public DataSourceProperties connectionProp2() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("mysql.connection2")
public DataSource connection2() {
return connectionProp2().initializeDataSourceBuilder().build();
}

Eugen
- 877
- 6
- 16