Using Multiple Data Sources Using Spring Boot 2 and Spring Data

Gokuldas Puthenpurakkal
2 min readFeb 16, 2019

--

Introduction

I had once a typical requirement of handling multiple datasources in the same spring boot project. As you know spring boot auto configures the JPA datasource once we give the spring.jpa.<properties> configuration in the property file. This configuration will not work if have to connect to two datasources. eg: A database with name db1 and another database with name db2. These databases can be of the same type (db1,db2->postgres)or of different types (db1->postgres, db2->MySQL).

The process by which we configure the datasource is simple, yet a little tricky. Most of the available tutorials contatain how it is done for spring boot 1x versions, but there are few changes to be done when it comes to spring boot 2.

In this example we will be trying to configure two postgreSQL datasources of with names db1 and db2 respectively along with the configurations regarding how the default boot settings can overridden to fetch data from multiple datasources.

Setting up the project

Create a spring starter project using Spring Tools Suite or by using Spring Initializer project generator in the site https://start.spring.io

We will be using

  • Gradle BuildShip 3
  • Jar for packaging
  • Min Java Version set to 8 and
  • Spring Boot v2.1.3

In the next page add the following dependencies to the spring boot starter project

  • JPA
  • Web
  • PostgreSQL
  • Lombok
  • Actuator (Optional)
  • DevTools (Optional)

applicaton.properties

application.properties

We will prefix the database 1 configurations with value db1 and database 2 configuration with value db2

Custom Database Configurations

Since we are creating two separate database configurations, all the associated entity and repository mappings should be separated out into two distinct packages.

Db1 Packages
Model -> com.gokuldasputhenpurakkal.db1.model
Repository -> com.gokuldasputhenpurakkal.db1.repository
Db2 Packages
Model -> com.gokuldasputhenpurakkal.db2.model
Repository -> com.gokuldasputhenpurakkal.db2.repository

While overriding the configurations we need to define the following to perform various database related operations.

  • DataSource
  • EntityManagerFactory
  • TransactionManager

(For Spring Boot 2, Make sure you use the type of database as HikariDataSource.class). You configuration will not work if we follow the typical example for springboot1 while defining the datasource as the jdbc url and other properties will not be resolved in such a situation.

Db1Config
Db2Config

We should ensure that while creating two database configurations, one should be marked as primary. This is mandatory.

I have written two repositories namely Db1Repository and Db2Repository that returns the datbase name and number matched to a native query.

You can test the REST Endpoints using the following GET endpoints

GET| http://localhost:9001/api/db1{
"key": 1,
"databaseName": "db1"
}
GET| http://localhost:9001/api/db2{
"key": 2,
"databaseName": "db2"
}

You can find the full source code of the project in the following GitHub Link. Follow me on LinkedIn for more updates. Feel free to connect with me, if you have an interesting/challenging opportunity for me to explore.

--

--

Gokuldas Puthenpurakkal

Geek . Engineer . Traveller . Adventurer . Rider . Creative Soul . Humanitarian . Movie Buff