How to Configure Multiple Data Source in Spring Boot


Sharing is caring!

Overview

In my previous post, I wrote an article about configuring data source by using Hikari CP. However, there are times that we need more than one database connection in an application. Later in this post, I will show how to configure multiple data source by using spring boot.

Use Case

Recently, I have a requirement for my project to connect into two databases. The first connection goes to postgresql and the other one connects to mysql. Since I am a huge fan of spring boot with all its simplicity, I urged myself to stick with this option.

For single data source, it is very easy to configure. Just by mentioning all the connection properties in application.yml, everything just run as it is. But when you need more than one, you need more than just declare in properties file.

Step by step

The pom.xml file is no different with the previous post, you only need to add mysql library inside it.

Step 1: application.yml

Create a custom key properties for each data source.

Step 2: Configure Data Source

Next step is to create a Bean Configuration for each data source.

Take a notice in the highlighted part. First the @ConfigurationProperties annotation. The prefix is refer to the application.yml configuration (See postgres->datasource declaration). Another one is a basePackages property inside @EnableJpaRepositories annotation. This property is pointed to the postgresql repository package in your project structure. One ore thing is to define where my database models are located. The models are defined when I setup the EntityManagerFactory (See line 25).

Next do the same with mysql data source configuration. The main difference for mysql bean configuration is removing the @Primary annotation for each method declaration.

Step 4: Models and Repository

The last part is to create database models and spring data repository for each data source. For instance, in this article the postgresql will have the models class under com.rurocker.model.postgre and mysql under com.rurocker.model.mysql. And for the repository interfaces and/or classes, postgresql will be located under com.rurocker.repository.postgre meanwhile mysql will be under com.rurocker.repository.mysql.

And that’s it. You can execute mvn spring-boot:run to make it up and running.

Conclusion

Well, maybe it is not as simple as spring boot is meant to be. But in my opinion, this is still acceptable because the complexity is still low. Take note, this example does not cover distributed transaction for multiple data source. So careful must be taken when using this example.

That’s all for me right now. If you guys have found simpler and easier approach, I really would like to know and we can share each other.

Author: ru rocker

I am a professional software developer with more than 10 years experiences. I am a certified Java Developer (SCJP and SCWCD). However, In the recent months, I have more interest in DevOps and start to become a polyglot developer. Python and Go-lang become my favorite programming languages besides Java.

1 thought on “How to Configure Multiple Data Source in Spring Boot”

  1. Hi ru-rocker..It’s working fine in JPA. But i want to connect jdbctemplate how can i configure please give me the solution step by step

Leave a Reply

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