How to Configure Multiple Data Sources in a Spring Boot?

By | July 17, 2021

In this post, We will learn How to Configure Multiple Data Sources in a Spring Boot using a demo project?

There are many situations where we need to read/write data from multiple databases and while using Spring data JPA sometimes it becomes challenging to configure to get connections with multiple databases in a single application. Hence, in this post, we are going to discuss creating multiple database connections with Spring data JPA using spring boot through a single application. Actually, spring boot usually provides a very easy way to use multiple data sources in a single application using properties file configurations.

Here We are going to learn How to use MySQL and Oracle databases to get connections in a single Spring Boot application.

Below is the Project Structure created in STS(Spring Tool Suite):

pom.xml

Maven pom with Spring data JPA,mysql-connector-java, and ojdbc8(for Oracle) dependencies.

We have two different config files (MySQLDBConfig & OracleDBConfig) for two different data sources.

Following is the configuration to connect to MySQL database. We have configured the entitymanager required to query the MySQL database as per JPA.

@ConfigurationProperties(prefix = “spring.mysql.datasource”). This will ensure that spring picks properties starting with spring.mysql.datasource creating the data source and utilizes it while executing methods of UserRepository.java.

basePackages = “com.kkhindigyan.app.user.dao” will ensure that spring uses mysql datasource while executing methods of UserRepository.java

@Primary This annotation tells spring to use this bean to use as a primary bean as we have multiple beans for the same return type. To use other beans of the same return type we require to use @Qualifier annotation.

MySQLDBConfig.java

OracleDBConfig.java

Following is the configuration for Oracle Database  The configurations are similar to MySQLDBConfig.java.

Employee.java

Employee JPA Entity class

User.java

User JPA Entity class

Following is the DAO interfaces that is responsible to query against MySQL and Oracle databases. Both interfaces(EmployeeRepository & UserRepositoryextends CrudRepository which has different CRUD methods such as create, findOne, delete etc.. and hence our DAO’s interfaces automatically inherit them which is available for our service classes to use. It’s spring data that will generate the implementations at run time for these CRUD methods. Hence, we don’t have to provide the implementations.

Notice the generic parameters in CrudRepository. Based on these parameters, Spring data will perform different CRUD operations at run time on our behalf.

EmployeeRepository.java

UserRepository.java

Following are the service classes where we have injected both the DAOs. Here both the service classes communicate with the different DAOs and doing read/write operations from multiple databases.

UserService.java

EmployeeService.java

Following is the application.properties the file that contains configurations for both MySQL and Oracle databases. You can notice that properties starting from spring.mysql.datasource has MySQL database configuration and properties starting from spring.oracle.data source has Oracle data source configurations. These configurations are used in the above MySQLDBConfig.java & OracleDBConfig.java while configuring entitymanager and transactionmanager for respective database connections.

application.properties

hibernate.properties

SpringBootDataJpaWithMultipleDatabaseExampleApplication.java

If you run ClientTest.java as Spring Boot App then it will give the below output: 

That’s all about How to Configure Multiple Data Sources in a Spring Boot?

Github Link to Download Source Code

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.