This tutorial will show you how we can define multiple datasources in Spring Boot application. We have seen how to define multiple datasources in Spring application, but here we will define multiple datasources in Spring Boot application. There may be situations where you need to define multiple datasources in Spring Boot application.

For instance, you may need to define multiple datasources for your Liquibase applications.  We can have different datasource bean ids configured into the Spring configuration class and we can use those datasources as per our needs in the different classes when required by accessing the bean id reference.

Recommended reading: How to setup Liquibase in Spring for Multiple DataSources

Prerequisites

The following configurations are required in order to run the application

Eclipse
JDK 1.8
Have gradle installed and configured
Spring boot dependencies in build.gradle

Assuming your Spring Boot application is already configured using the above dependencies and you want to define multiple datasources in your application.

As a first step define your two or more datasources into application.properties file under classpath directory src/main/resources

spring.datasource.url=jdbc:Oracle:thin:@//<host>:<port>/<service name>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver

spring.secondaryDatasource.url=jdbc:Oracle:thin:@//<host>:<port>/<service name>
spring.secondaryDatasource.username=<username>
spring.secondaryDatasource.password=<password>
spring.secondaryDatasource.driverClassName=oracle.jdbc.driver.OracleDriver

In the above application.properties file you see we have added two datasources of Oracle type. If you want you can also add different database type datasources and more than even two datasources.

Now we need to define bean for different datasources declared into application.properties. So we will create a Spring configuration class  DatabaseConfig annotated with @Configuration.

In the below Spring configuration class we have defined two beans for two different datasources. You can also assigned name to each bean so that it would be easier for you to work with the different datasources at Repository or DAO layer.

We are referring the different datasource properties using Spring @ConfigurationProperties annotation.

@Configuration
public class DatabaseConfig {

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

That’s all. Hope you got idea how to define multiple datasources in Spring Boot application.

You may also like to read define multiple datasources in Spring application.

Recommended reading: How to setup Liquibase in Spring for Multiple DataSources

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *