spring-multi-data-source

Dhiman Seal

Automation Engineer
Software Architect
Software Engineer
Java
MySQL
Spring Boot

spring-multi-data-source

Spring Boot has multiple limitations when using multiple data sources in a single service. This project aims to solve those limitations by providing custom annotations that can be used to generate the required Bean-providing configuration classes and repositories during the build process itself, which the service can then use.
The best part is that the entirety of the generated code is clean, human-readable, and can be directly carried over to the relevant packages of the main code if you no longer wish to be tied down to this library in the future.

Introduction

The limitations of using multiple data sources in a single service in Spring are:
We need to split the packages of repositories to allow one @EnableJpaRepositories mapped to one package for each data source.
There is a lot of boilerplate config generation involved to create beans of data sources, entity managers, transaction managers etc. for each data source.
To get EntityManagerFactoryBuilder injected, we need to declare one of the data sources and all its beans as @Primary. Otherwise, the service won't even start up.
To mitigate the above limitations, I have created two custom annotations in Java that can be used to configure multi-data source configurations for a service. Let's break down each annotation:

Annotations Provided

@EnableMultiDataSourceConfig

This annotation is used to enable multi-data source configuration for the service. This will replace the @EnableJpaRepositories and @EntityScan annotations used by Spring.\

@EnableMultiDataSourceConfig.DataSourceConfig

This sub-annotation is used to configure a data source and its properties for @EnableMultiDataSourceConfig. It can not be applied directly anywhere other than in the dataSourceConfigs attribute of @EnableMultiDataSourceConfig.

@TargetSecondaryDataSource

This annotation is used to create copies of repositories in relevant packages and autoconfigure them to use the relevant data sources.
Both annotations are available at the source level and are not retained at runtime. They are intended to be used for generating code for configuring data sources during the build process.

Removing Dependency on spring-multi-data-source without Losing Functionality

A big selling point of this library is that it is not a black box. The generated code is clean, human-readable, and can be directly carried over to the relevant packages of the main code if you no longer wish to be tied down to this library.
Move the generated configuration classes and repositories to the relevant packages in your project from the target/generated-sources/annotations directory.
Remove implements IMultiDataSourceConfig from the generated @Configuration classes.
Remove the @EnableMultiDataSourceConfig annotation from your configuration class.
Remove the @TargetSecondaryDataSource annotation from your repository methods.
Remove the spring-multi-data-source dependency from your project pom.
And that's all you have to do! You are no longer tied down to this library and have the freedom to use and modify the generated code to your liking.

Contributing

Please feel free to raise issues and submit pull requests. Please check out CONTRIBUTING.md for more details.

License

This project is licensed under the GNU Lesser General Public License v3.0. Please check out LICENSE for more details.

Resources

Partner With Dhiman
View Services

More Projects by Dhiman