Multiple Datasources With Spring Boot Data JPA
Tags: java, springboot, springdata, jpa
Published: 2021-02-10 03:32:53

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](https://dev-to-uploads.s3.amazonaws.com/i/y6mapx3l5lyh1sy21ydg.png)

### 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](https://javatodev.com/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](https://javatodev.com/lombok-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 UserEntity.java and paste following content to that file,

{% gist https://gist.github.com/javatodev/4d8bcaf7bb317a452616629231e668da %}

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

{% gist https://gist.github.com/javatodev/b487ef98d6a81106a0f1cec5404e85ca %}

## 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 AccountEntity.java in ** com.javatodev.api.model.bank.account** and copy following content,

{% gist https://gist.github.com/javatodev/106618114e8335385d9f092bf41105b3 %}

Then create CreditCardEntity.java in com.javatodev.api.model.bank.card and add following content to it.

{% gist https://gist.github.com/javatodev/5eee79f01f5e60484214c93214e479b1 %}

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

{% gist https://gist.github.com/javatodev/ec3b4fa494529fe9b4316a54ae6cd41d %}

{% gist https://gist.github.com/javatodev/3cdf73b4729c47a5a08e218049333ce0 %}

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

![Database Access Entities and Repositories](https://dev-to-uploads.s3.amazonaws.com/i/vfvc3b07vkta0evz85of.png)

## 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 application.properties and here we are setting datasource connection properties for each database.

{% gist https://gist.github.com/javatodev/55af25bebf246eefe20f0b555910101a %}

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 BankDataSourceConfiguration.java 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 com.javatodev.api.repository.bank. 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.

{% gist https://gist.github.com/javatodev/7a86b7a95d7d164925e03e8bb522ca01 %}

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.

{% gist https://gist.github.com/javatodev/d18abba0604fa1b303e2f1974f70e62a %}

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

{% gist https://gist.github.com/javatodev/6800407bf122ed5f3807422370cbf7f5 %}

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

{% gist https://gist.github.com/javatodev/a9963b753c07c14bdfb56fd07bde55f1 %}

All done now our whole configuration should look like below,

{% gist https://gist.github.com/javatodev/e0995e00a6bee4dbe31a2c5bbbe610d3 %}

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

{% gist https://gist.github.com/javatodev/a91082bd0b003d385bc8a57df34adb22 %}

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.

{% gist https://gist.github.com/javatodev/0704c4e6b544f7e4a34fa5ce178be034 %}

test output

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2v8n0janqhsvyae5y1a6.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p2uplarh6w39wg0qqr9c.png)

## Conclusion

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](https://javatodev.com/list-of-articles/) 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](https://github.com/javatodev/spring-boot-multiple-datasources) repository.