DEV Community

Cover image for Multiple Datasources With Spring Boot Data JPA

Posted on • Originally published at

Multiple Datasources With Spring Boot Data JPA

Hello readers, In this article I’m going to explain how we can use multiple data sources in Spring Boot application which uses Spring Data JPA with practical usage.

Technologies going to use,

  • Java 1.8
  • Spring Boot: 2.3.4.RELEASE
  • Spring Data JPA
  • PostgreSQL
  • Lombok
  • Gradle
  • Intellij Idea for IDE

Where We Can Use This Solution?

Let’s think your spring boot application should use two databases in the same instance, to save different data sets. In that case, we can give spring data JPA a chance to identify multiple datasources, and data JPA will use those datasource connections to access those different databases.

Here I’ll explain this tutorial with the following scenario where users, accounts, credit cards should be saved in different databases.

Multiple Datasource Spring Boot Diagram

Solution In Brief

Here we will create two data source configurations for User DB and Bank DB. Additionally, we will introduce different database connection parameters in application properties and use those in mentioned data source configurations and we should let spring boot identify repository layer and entity layer for each data source config.

Create Spring Boot Application

Here I’m using spring initializr to generate a spring boot project with all the dependencies I need for this tutorial. If you are really new to Spring Boot, Please follow our article on How to Create a Spring Boot Project.

Here I’ve selected following dependencies to create spring boot project using spring initilizr,

  • Spring Web – contains common web-specific utilities for both Servlet and Portlet environments.
  • Lombok – The coolest plugin to spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully-featured builder, Automate your logging variables, and much more.
  • Spring Data JPA – JPA with Spring Data
  • PostgreSQL – PostgreSQL driver for spring boot.

If you need to learn how we can use Lombok in spring boot follow our article Guide to use Lombok In Spring Boot.

Defining User Database Specific Models and Repositories

Here we will create the model classes and repositories for user database-specific tables.

first, create a package in root level (com.javatodev.api in my example) named as a model and create a and paste following content to that file,

Then create another package named as com.javatodev.api.repository.user, and add following

Defining Bank Database Specific Models and Repositories

Now we are ready with user DB specific changes. Let’s focus on Bank specific models and repositories.

Here do the same what we did above in creating user-specific models and repositories. Additionally keep in mind to create those inside a package called bank since we need to separate Bank specific models, repositories with User specific ones.

create in **** and copy following content,

Then create in and add following content to it.

Then finally create the repository classes for both Account and Credit Card entities as below,

All done now we have our JPA level configurations setup that we should have in order to access databases.

Database Access Entities and Repositories

Configuring Multiple Datasources In Spring Boot Application

Now we should write custom datasource configuration with introducing custom datasource connection properties.

first copy following into the and here we are setting datasource connection properties for each database.

As you see here we not setting default JPA database connection URL, we are setting additional identifiers as bank and user while setting database connection properties inside this property file.

Then create configuration package in com.javatodev.api.configuration, and create inside that package and copy following content to it.

Then introduce @EnableJpaRepositories annotation to introduce package where this datasource should get repositories to access database. Here it is Then in addition to that, we should introduce entity manage reference and transaction manager reference, Just add it as below for the moment and will discuss it below.

Then add following @Bean definition into the same configuration file, Here it will capture url, username, password and driver-class-name which we defined in application properties file under bank.

Then create the datasouce using DatasourceProperties method which we have written earlier as below,

Finally, create EntityManagerFactory and TransactionManagerFactory as below, Here we should show where this EntityManager should find Entity classes inside this application. Here It is

All done now our whole configuration should look like below,

Then create another class named as and setup user specific datasources as below.

All done now our application could use multiple datasources in order to access multiple databases using spring data JPA, and let’s test that with simple Junit test.

test output

Alt Text

Alt Text


Thanks for reading our latest article on How To Configure Multiple Datasources With Spring Boot Data JPA with practical usage.

If you are looking for spring boot based practical application development tutorials, just check our article series and comment on whatever the new things you need to see in our website.

You can get source code for this tutorial from our GitHub repository.

Top comments (0)