Spring Boot is a popular framework for building Java-based applications, and it provides a lot of out-of-the-box functionality for connecting to and managing data sources. One of the most powerful features of Spring Boot is the ability to configure and use multiple data sources in your application. In this blog post, we will go through the process of setting up and using multiple data sources in a Spring Boot application.



Configure Multiple DataSource Bean

The first step in setting up multiple data sources in a Spring Boot application is to define the data sources themselves. This can be done by creating separate DataSource beans for each data source in your application. For example, if you have two data sources named “ds1” and “ds2”, you would create two DataSource beans like this:

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

@Bean
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public DataSource ds2DataSource() {
    return DataSourceBuilder.create().build();
}

The @Primary annotation on the first bean tells Spring Boot that this is the default data source to be used in the application.



Configure Entity using @EntityScan

Next, you will need to configure the Entity that will be used with each data source. This can be done by using the @EntityScan annotation on a @Configuration class. For example, if you have two packages containing entities for each data source, you would use the @EntityScan annotation like this:

@Configuration
@EntityScan(basePackages = {"com.example.ds1.entities", "com.example.ds2.entities"})
public class MultiDataSourceConfig {
    // ...
}


Configure JPARepository interface

Once the data sources and entities are set up, you can use the JpaRepository interface to interact with the data in your application. You can use the @EnableJpaRepositories annotation to enable JPA repositories for both data sources in your application:

@Configuration
@EnableJpaRepositories(basePackages = {"com.example.ds1.repositories", "com.example.ds2.repositories"},
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager")
public class MultiDataSourceConfig {
    // ...
}


Configure EntityManagerFactory and JpaTransactionManager

Finally, you will need to configure the Entity manager factory and transaction manager for your application. You can do this by creating separate LocalContainerEntityManagerFactoryBean and JpaTransactionManager beans for each data source:

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean ds1EntityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("ds1DataSource") DataSource dataSource) {
    return builder
            .dataSource(dataSource)
            .packages("com.example.ds1.entities")
            .persistenceUnit("ds1")
            .build();
}

@Bean
public LocalContainerEntityManagerFactoryBean ds2EntityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("ds2DataSource") DataSource dataSource) {
    return builder
            .dataSource(dataSource)
            .packages
            .("com.example.ds2.entities")  
            .persistenceUnit("ds2")
            .build();
}

@Bean
@Primary
public JpaTransactionManager ds1TransactionManager(
@Qualifier("ds1EntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

@Bean
public JpaTransactionManager ds2TransactionManager(
@Qualifier("ds2EntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

With this configuration, you can now use the JpaRepository interface to interact with both data sources in your application. You can use the @Transactional annotation to define which data source should be used for a particular repository method, or you can use the @Qualifier annotation to specify which data source should be used for a particular bean.



Conclusion

In conclusion, setting up and using multiple data sources in a Spring Boot application is a straightforward process. By properly configuring your data sources, entities, repositories, and transaction managers, you can easily interact with multiple data sources in your application. With the help of Spring Boot’s powerful features, you can easily manage and maintain your application’s data sources, ensuring that your application remains stable and performant.



Leave a Reply