Spring Boot Configure and Use Two DataSources Spring Boot Configure and Use Two DataSources spring spring

Spring Boot Configure and Use Two DataSources


Here you go.

Add in your application.properties file:

#first dbspring.datasource.url = [url]spring.datasource.username = [username]spring.datasource.password = [password]spring.datasource.driverClassName = oracle.jdbc.OracleDriver#second db ...spring.secondDatasource.url = [url]spring.secondDatasource.username = [username]spring.secondDatasource.password = [password]spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

Add in any class annotated with @Configuration the following methods:

@Bean@Primary@ConfigurationProperties(prefix="spring.datasource")public DataSource primaryDataSource() {    return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix="spring.secondDatasource")public DataSource secondaryDataSource() {    return DataSourceBuilder.create().build();}


Update 2018-01-07 with Spring Boot 1.5.8.RELEASE

Most answers do not provide how to use them (as datasource itself and as transaction), only how to config them.

You can see the runnable example and some explanation in https://www.surasint.com/spring-boot-with-multiple-databases-example/

I copied some code here.

First you have to set application.properties like this

#Databasedatabase1.datasource.url=jdbc:mysql://localhost/testdbdatabase1.datasource.username=rootdatabase1.datasource.password=rootdatabase1.datasource.driver-class-name=com.mysql.jdbc.Driverdatabase2.datasource.url=jdbc:mysql://localhost/testdb2database2.datasource.username=rootdatabase2.datasource.password=rootdatabase2.datasource.driver-class-name=com.mysql.jdbc.Driver

Then define them as providers (@Bean) like this:

@Bean(name = "datasource1")@ConfigurationProperties("database1.datasource")@Primarypublic DataSource dataSource(){    return DataSourceBuilder.create().build();}@Bean(name = "datasource2")@ConfigurationProperties("database2.datasource")public DataSource dataSource2(){    return DataSourceBuilder.create().build();}

Note that I have @Bean(name="datasource1") and @Bean(name="datasource2"), then you can use it when we need datasource as @Qualifier("datasource1") and @Qualifier("datasource2") , for example

@Qualifier("datasource1")@Autowiredprivate DataSource dataSource;

If you do care about transaction, you have to define DataSourceTransactionManager for both of them, like this:

@Bean(name="tm1")@Autowired@PrimaryDataSourceTransactionManager tm1(@Qualifier ("datasource1") DataSource datasource) {    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);    return txm;}@Bean(name="tm2")@AutowiredDataSourceTransactionManager tm2(@Qualifier ("datasource2") DataSource datasource) {    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);    return txm;}

Then you can use it like

@Transactional //this will use the first datasource because it is @primary

or

@Transactional("tm2")

This should be enough. See example and detail in the link above.


Refer the official documentation


Creating more than one data source works same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections).

@Bean@Primary@ConfigurationProperties(prefix="datasource.primary")public DataSource primaryDataSource() {    return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix="datasource.secondary")public DataSource secondaryDataSource() {    return DataSourceBuilder.create().build();}