{{announcement.body}}
{{announcement.title}}

Spring Boot 2 With Multiple Datasources

DZone 's Guide to

Spring Boot 2 With Multiple Datasources

In this article, see how your Spring Boot application can interact with multiple datasources.

· Database Zone ·
Free Resource

Person wearing boots with flowers sticking out

Spring Boot 2

In this article, I’m going to explain how your Spring Boot application can interact with multiple datasources and not necessarily the same type (Postgres for this demo), but it can be applied across the other relational databases. There are cases that you need to have multiple datasources from different vendors, but the general concept is similar, and this example can be useful with a few changes in the project configuration (application.yml).

For this demo, I chose PostgresSQL Data Replication, which is common in a high load database with high traffic in the application.

There are times that even having the best database (PostgresSQL, Oracle, MySQL, .. ) Tuning can not be as help-full as much as When you separate Read DB and Writes DB in Application Level.

Postgres Setup

For this demo, you need two separate Postgres databases where one is the master and the other one is the replica.

I have used two PostgresSQL databases, which are running on my local Docker on two separate ports: 5432 and 5433.

You might also want to read:  Set up Multiple DataSources With Spring Boot and Spring Data in PCF

For simplicity, just run: docker-compose up --force-recreate.

The docker-compose.yml is already in the project, which contains two PostgresSQL databases in two different ports, with demo database.

Note: You can always uninstall it as: docker-compose down if you need to.

YAML
xxxxxxxxxx
1
24
 
1
version: '3.1'
2
services:
3
  db1:
4
    image: postgres
5
    container_name: postgres1
6
    volumes:
7
      - ./postgres-data1:/var/lib/postgresql/data
8
    ports:
9
      - "5432:5432"
10
    environment:
11
      POSTGRES_PASSWORD: postgres_user_for_db_write
12
      POSTGRES_USER: postgres
13
      POSTGRES_DB: demo
14
  db2:
15
    image: postgres
16
    container_name: postgres2
17
    volumes:
18
      - ./postgres-data2:/var/lib/postgresql/data
19
    ports:
20
      - "5433:5432"
21
    environment:
22
      POSTGRES_PASSWORD: postgres_user_for_db_read
23
      POSTGRES_USER: postgres
24
      POSTGRES_DB: demo


Spring Boot Setup

From https://start.spring.io/, select web, data-jpa, lombok, postgresDriver.

Once you generate and download the zip file, you should have similar POM file as:

XML
xxxxxxxxxx
1
64
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5
    <parent>
6
        <groupId>org.springframework.boot</groupId>
7
        <artifactId>spring-boot-starter-parent</artifactId>
8
        <version>2.2.2.RELEASE</version>
9
        <relativePath></relativePath> <!-- lookup parent from repository -->
10
    </parent>
11
    <groupId>com.example</groupId>
12
    <artifactId>demo</artifactId>
13
    <version>0.0.1-SNAPSHOT</version>
14
    <name>demo</name>
15
    <description>Demo project for Spring Boot</description>
16
 
          
17
    <properties>
18
        <java.version>1.8</java.version>
19
    </properties>
20
 
          
21
    <dependencies>
22
        <dependency>
23
            <groupId>org.springframework.boot</groupId>
24
            <artifactId>spring-boot-starter-web</artifactId>
25
        </dependency>
26
        <dependency>
27
            <groupId>org.springframework.boot</groupId>
28
            <artifactId>spring-boot-starter-data-jpa</artifactId>
29
        </dependency>
30
        <dependency>
31
            <groupId>org.springframework.boot</groupId>
32
            <artifactId>spring-boot-configuration-processor</artifactId>
33
        </dependency>
34
        <dependency>
35
            <groupId>org.projectlombok</groupId>
36
            <artifactId>lombok</artifactId>
37
            <optional>true</optional>
38
        </dependency>
39
        <dependency>
40
            <groupId>org.postgresql</groupId>
41
            <artifactId>postgresql</artifactId>
42
            <scope>runtime</scope>
43
        </dependency>
44
        <dependency>
45
            <groupId>org.springframework.boot</groupId>
46
            <artifactId>spring-boot-starter-test</artifactId>
47
            <scope>test</scope>
48
            <exclusions>
49
                <exclusion>
50
                    <groupId>org.junit.vintage</groupId>
51
                    <artifactId>junit-vintage-engine</artifactId>
52
                </exclusion>
53
            </exclusions>
54
        </dependency>
55
    </dependencies>
56
    <build>
57
        <plugins>
58
            <plugin>
59
                <groupId>org.springframework.boot</groupId>
60
                <artifactId>spring-boot-maven-plugin</artifactId>
61
            </plugin>
62
        </plugins>
63
    </build>
64
</project>


pom.xml

For this demo, I used HikariDataSource as a default connection pool library by Spring Boot 2.2.2.

We need to have two separate datasources and EntityManagers, one for the writes (Master/Primary) and one for Reads (Slave/Secondary).

application.yml

YAML
xxxxxxxxxx
1
24
 
1
spring:
2
  datasource-write:
3
    driver-class-name: org.postgresql.Driver
4
    jdbc-url: jdbc:postgresql://localhost:5432/demo
5
    username: 'postgres'
6
    password: 'postgres_pass_for_db_write'
7
    platform: postgresql
8
    hikari:
9
      idle-timeout: 10000
10
      maximum-pool-size: 10
11
      minimum-idle: 5
12
      pool-name: WriteHikariPool
13
 
          
14
  datasource-read:
15
    driver-class-name: org.postgresql.Driver
16
    jdbc-url: jdbc:postgresql://localhost:5433/demo
17
    username: 'postgres'
18
    password: 'postgres_pass_for_db_read'
19
    platform: postgresql
20
    hikari:
21
      idle-timeout: 10000
22
      maximum-pool-size: 10
23
      minimum-idle: 5
24
      pool-name: ReadHikariPool

Since both DataSourceConfigWrite and DataSourceConfigRead are taking their configs from: “spring.datasource-write” and “spring.datasource-read”, the entityManagerFactory for each datasource can not get JPA configurations from application.yml . Thats why JPA configurations are added later on from static Property “JPA_PROPERTIES”. You can also add independent @ConfigurationProperties(“spring.jpa”) to supply your JPA configs based on your spring profile.

As you can see, I have two datasources as datasource-write and datasource-read with their own credentials.

DataSource configurations for WriteDB:

Java
xxxxxxxxxx
1
57
 
1
package com.ehsaniara.multidatasource.configurations;
2
 
          
3
import com.zaxxer.hikari.HikariConfig;
4
import com.zaxxer.hikari.HikariDataSource;
5
import org.hibernate.jpa.HibernatePersistenceProvider;
6
import org.springframework.boot.context.properties.ConfigurationProperties;
7
import org.springframework.context.annotation.Bean;
8
import org.springframework.context.annotation.Configuration;
9
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
10
import org.springframework.orm.jpa.JpaTransactionManager;
11
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12
import org.springframework.transaction.PlatformTransactionManager;
13
import org.springframework.transaction.annotation.EnableTransactionManagement;
14
 
          
15
import javax.persistence.EntityManagerFactory;
16
 
          
17
import static com.ehsaniara.multidatasource.DemoApplication.JPA_PROPERTIES;
18
import static com.ehsaniara.multidatasource.DemoApplication.MODEL_PACKAGE;
19
 
          
20
/**
21
 * @author Jay Ehsaniara, Dec 30 2019
22
 */
23
@Configuration
24
@ConfigurationProperties("spring.datasource-write")
25
@EnableTransactionManagement
26
@EnableJpaRepositories(
27
        entityManagerFactoryRef = "entityManagerFactoryWrite",
28
        transactionManagerRef = "transactionManagerWrite",
29
        basePackages = {"com.ehsaniara.multidatasource.repository.writeRepository"}
30
)
31
public class DataSourceConfigWrite extends HikariConfig {
32
 
          
33
    public final static String PERSISTENCE_UNIT_NAME = "write";
34
 
          
35
    @Bean
36
    public HikariDataSource dataSourceWrite() {
37
        return new HikariDataSource(this);
38
    }
39
 
          
40
    @Bean
41
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryWrite(
42
            final HikariDataSource dataSourceWrite) {
43
 
          
44
        return new LocalContainerEntityManagerFactoryBean() {{
45
            setDataSource(dataSourceWrite);
46
            setPersistenceProviderClass(HibernatePersistenceProvider.class);
47
            setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
48
            setPackagesToScan(MODEL_PACKAGE);
49
            setJpaProperties(JPA_PROPERTIES);
50
        }};
51
    }
52
 
          
53
    @Bean
54
    public PlatformTransactionManager transactionManagerWrite(EntityManagerFactory entityManagerFactoryWrite) {
55
        return new JpaTransactionManager(entityManagerFactoryWrite);
56
    }
57
}


DataSource Configurations for ReadDB:

Java
xxxxxxxxxx
1
58
 
1
package com.ehsaniara.multidatasource.configurations;
2
 
          
3
import com.zaxxer.hikari.HikariConfig;
4
import com.zaxxer.hikari.HikariDataSource;
5
import org.hibernate.jpa.HibernatePersistenceProvider;
6
import org.springframework.boot.context.properties.ConfigurationProperties;
7
import org.springframework.context.annotation.Bean;
8
import org.springframework.context.annotation.Configuration;
9
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
10
import org.springframework.orm.jpa.JpaTransactionManager;
11
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12
import org.springframework.transaction.PlatformTransactionManager;
13
import org.springframework.transaction.annotation.EnableTransactionManagement;
14
 
          
15
import javax.persistence.EntityManagerFactory;
16
 
          
17
import static com.ehsaniara.multidatasource.DemoApplication.JPA_PROPERTIES;
18
import static com.ehsaniara.multidatasource.DemoApplication.MODEL_PACKAGE;
19
 
          
20
/**
21
 * @author Jay Ehsaniara, Dec 30 2019
22
 */
23
@Configuration
24
@ConfigurationProperties("spring.datasource-read")
25
@EnableTransactionManagement
26
@EnableJpaRepositories(
27
        entityManagerFactoryRef = "entityManagerFactoryRead",
28
        transactionManagerRef = "transactionManagerRead",
29
        basePackages = {"com.ehsaniara.multidatasource.repository.readRepository"}
30
)
31
public class DataSourceConfigRead extends HikariConfig {
32
 
          
33
    public final static String PERSISTENCE_UNIT_NAME = "read";
34
 
          
35
 
          
36
    @Bean
37
    public HikariDataSource dataSourceRead() {
38
        return new HikariDataSource(this);
39
    }
40
 
          
41
    @Bean
42
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryRead(
43
            final HikariDataSource dataSourceRead) {
44
 
          
45
        return new LocalContainerEntityManagerFactoryBean() {{
46
            setDataSource(dataSourceRead);
47
            setPersistenceProviderClass(HibernatePersistenceProvider.class);
48
            setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
49
            setPackagesToScan(MODEL_PACKAGE);
50
            setJpaProperties(JPA_PROPERTIES);
51
        }};
52
    }
53
 
          
54
    @Bean
55
    public PlatformTransactionManager transactionManagerRead(EntityManagerFactory entityManagerFactoryRead) {
56
        return new JpaTransactionManager(entityManagerFactoryRead);
57
    }
58
}


Read and Write repositories should be in a separated package:

Java
xxxxxxxxxx
1
10
 
1
package com.ehsaniara.multidatasource.repository.writeRepository;
2
 
          
3
import com.ehsaniara.multidatasource.model.Customer;
4
import org.springframework.data.repository.CrudRepository;
5
 
          
6
/**
7
 * @author Jay Ehsaniara, Dec 30 2019
8
 */
9
public interface CustomerWriteRepository extends CrudRepository<Customer, Long> {
10
}


Java
xxxxxxxxxx
1
10
 
1
package com.ehsaniara.multidatasource.repository.readRepository;
2
 
          
3
import com.ehsaniara.multidatasource.model.Customer;
4
import org.springframework.data.repository.CrudRepository;
5
 
          
6
/**
7
 * @author Jay Ehsaniara, Dec 30 2019
8
 */
9
public interface CustomerReadRepository extends CrudRepository<Customer, Long> {
10
}


You also need to set:

Java
xxxxxxxxxx
1
24
 
1
package com.ehsaniara.multidatasource;
2
 
          
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
 
          
6
import java.util.Properties;
7
 
          
8
@SpringBootApplication
9
public class DemoApplication {
10
 
          
11
    public final static String MODEL_PACKAGE = "com.ehsaniara.multidatasource.model";
12
 
          
13
    public final static Properties JPA_PROPERTIES = new Properties() {{
14
        put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL10Dialect");
15
        put("hibernate.hbm2ddl.auto", "update");
16
        put("hibernate.ddl-auto", "update");
17
        put("show-sql", "true");
18
    }};
19
 
          
20
 
          
21
    public static void main(String[] args) {
22
        SpringApplication.run(DemoApplication.class, args);
23
    }
24
}


And the actual logics are in the service layer:

Java
xxxxxxxxxx
1
45
 
1
package com.ehsaniara.multidatasource.service;
2
 
          
3
import com.ehsaniara.multidatasource.model.Customer;
4
import com.ehsaniara.multidatasource.repository.readRepository.CustomerReadRepository;
5
import com.ehsaniara.multidatasource.repository.writeRepository.CustomerWriteRepository;
6
import org.springframework.stereotype.Service;
7
import org.springframework.util.Assert;
8
 
          
9
import java.util.Optional;
10
 
          
11
/**
12
 * @author Jay Ehsaniara, Dec 30 2019
13
 */
14
@Service
15
public class CustomerServiceImpl implements CustomerService {
16
 
          
17
    private final CustomerReadRepository customerReadRepository;
18
    private final CustomerWriteRepository customerWriteRepository;
19
 
          
20
    public CustomerServiceImpl(CustomerReadRepository customerReadRepository, CustomerWriteRepository customerWriteRepository) {
21
        this.customerReadRepository = customerReadRepository;
22
        this.customerWriteRepository = customerWriteRepository;
23
    }
24
 
          
25
    public Optional<Customer> getCustomer(Long id) {
26
        return customerReadRepository.findById(id);
27
    }
28
 
          
29
    public Customer createCustomer(Customer customer) {
30
 
          
31
        Assert.notNull(customer, "Invalid customer");
32
        Assert.isNull(customer.getId(), "customer id should be null");
33
        Assert.notNull(customer.getName(), "Invalid customer name");
34
 
          
35
        return customerWriteRepository.save(customer);
36
    }
37
 
          
38
    public Customer updateCustomer(Customer customer) {
39
 
          
40
        Assert.notNull(customer, "Invalid customer");
41
        Assert.notNull(customer.getId(), "Invalid customer id");
42
 
          
43
        return customerWriteRepository.save(customer);
44
    }
45
}


When you run the application, you can see two Persistence Units as Read and Write.

Now if run, this line you create customer in DB1 to create a customer recored (Write in DB1):

Shell
xxxxxxxxxx
1
 
1
curl -H "Content-Type: application/json" --request POST --data '{"name":"Jay"}'   http://localhost:8080/customer


OR

to update the customer recored (Write in DB1):

Shell
xxxxxxxxxx
1
 
1
curl -H "Content-Type: application/json" --request PUT --data '{"id":1 , "name":"Jay ehsaniara"}'   http://localhost:8080/customer


But if you run this line, you get data from DB2 to read from DB2:

Shell
xxxxxxxxxx
1
 
1
 curl --request GET  http://localhost:8080/customer/1


Note: You need to insert customer manually in DB2 since it has no pre customer. And we haven't set up Postgres Replication yet.

I have added the project source code in GitHub.

Good luck!

Further Reading

Multiple Databases With Shared Entity Classes in Spring Boot and Java

Multiple MongoDB Connectors With Spring Boot

Topics:
spring boot 2.2 ,java ,datasource ,postgres database ,jpa ,hibernate ,database ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}