Configuring multiple data sources with Spring Boot 2 and Spring Data JPA

Afrar Malakooth
3 min readJul 26, 2020

--

Photo by Julia Lobkova on Unsplash

Welcome to another Spring Boot tutorial. If you’re totally new to Spring Boot check out my starter tutorial Setting up your first Spring Boot app on Medium. For this story I’ll be covering how to configure multiple data sources with Spring Data JPA. In next couple of stories I’m planning to demonstrate how to model entities with Spring Data JPA and schedule jobs with Quartz etc.

To get started you need to setup a project with two databases configured. For this tutorial I’ll be choosing a locally installed MySQL database and embedded H2 database, apparently this should work with any database provider. From now onwards I’ll be referring MySQL database as the Primary Data Source and H2 database as the Secondary Data Source. On your browser head on to https://start.spring.io and start configuring the Spring Boot project.

Spring Initializr

Once you’re done with the configuration click on the Generate button and download the project. Unzip the downloaded file and open it in your favorite IDE. Then open application.properties file and add below properties. If you have ever setup a Spring Boot project with a single data source, you might have known that it will auto configure and allow us to simply use spring.datasource.* properties to provide necessary parameters. But that’s not the case with configuring multiple datasources.

# Primary data source configuration
spring.datasource-primary.url=jdbc:mysql://localhost:3306/primarydb
spring.datasource-primary.username
=root
spring.datasource-primary.password
=root
spring.datasource-primary.driverClassName
=com.mysql.cj.jdbc.Driver

# Secondary data source configuration
spring.datasource-secondary.url=jdbc:h2:mem://localhost:8082/secondarydb
spring.datasource-secondary.username
=sa
spring.datasource-secondary.password
=
spring.datasource-secondary.driverClassName=org.h2.Driver

To configure multiple data sources in Spring Data JPA we need to group the Model classes and Repository interfaces for each data source under different packages, this is how we tell Spring what belongs to which data source. Assuming that package names given in below example and comments itself are self explanatory. You need to have at least two packages (which means that you can have multiple packages for a single data source) for each data source, one for Model classes and another for Repository interfaces.

// Model classes for primary data source
package com.example.demo.model.primary;
// Model classes for secondary data source
package com.example.demo.model.secondary;
// Repository interfaces for primary data source
package com.example.demo.repository.primary;
// Repository interfaces for secondary data source
package com.example.demo.repository.secondary;

In the case of a single data source Spring Boot itself will take care of all the configurations. But when we setup multiple data sources we should give instructions to Spring on how to configure the data sources. Refer the configuration classes below and make sure to provide the property identifiers and package names as you have defined in the project.

Primary Data Source Configuration
Secondary Data Source Configuration

It’s mandatory to annotate one data source with @Primary annotation. Also Spring Boot 2.x uses HikariCP for connection pooling unlike Spring Boot 1.x. When we configure multiple data sources we can’t anymore specify the JPA properties like dialect and ddl.auto in the properties file, those should be included in the configuration classes separately for each data source.

Happy coding with Spring Boot! I’ll be publishing a video based on this story. Above is a DEV Community video I published and you might be interested to check my Medium story Creating a simple browser for iOS using WKWebView with Swift.

--

--

Afrar Malakooth
Afrar Malakooth

Written by Afrar Malakooth

An energetic Software Engineer with 5+ years experience of writing code for leading enterprises. Passionate about social work, volunteering and travelling.

Responses (2)