How to setup Liquibase in Spring for Multiple DataSources

Introduction

In this tutorial I will show you how to setup Liquibase in Spring for multiple datasources. In my previous tutorial I had shown how to setup multiple datadources in Spring Boot application. Here also I am going to create a Spring Boot application to define multiple datasources and will use these datasources into Liquibase.

I had created examples using XML and YAML & SQL on how to work with Liquibase for evolving your database and I used single database in those examples but here I will tell you how you can use multiple datasources.

Prerequisites

Eclipse, Java, Spring Boot, Gradle or Maven, Liquibase, Any Database

Setting up Project

The first step is to create and setup the gradle or maven based Spring Boot project in Eclipse. Create a gradle based project in Eclipse.

You can check examples using XML and YAML & SQL to setup maven or gradle build file.

DataSource and Liquibase Configuration

As a next step declare your two or more datasources into application.properties file under classpath directory src/main/resources.

You need to also include separate liquibase configuration files when you want to use separate datasource for separate liquibase.

It depends on you whether you want to use YAML & SQL based configuration or XML based configuration for your Liquibase.

#datasource 1
spring.datasource.url=jdbc:Oracle:thin:@//<host>:<port>/<service name>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.primaryLiquibase.liquibase.change-log=classpath:/db/changelog-master-primary.yaml

#datasource 2
spring.secondaryDatasource.url=jdbc:Oracle:thin:@//<host>:<port>/<service name>
spring.secondaryDatasource.username=<username>
spring.secondaryDatasource.password=<password>
spring.secondaryDatasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.secondaryLiquibase.liquibase.change-log=classpath:/db/changelog-master-secondary.yaml

Create Changelog Files

For Liquibase to work you need to create changelog master file. For multiple datasources you need to create multiple changelog files.

Create changelog-master-primary.yaml

Create the below liquibase configuration file – changelog-master-primary.yaml – under classpath directory src/main/resources/db.

databaseChangeLog:
  - changeSet:
      id: selectCountry
      author: Soumitra
      changes:
      - sqlFile:
          dbms: oracle
          encoding: utf8
          endDelimiter: \n/\s*\n|\n/\s*$
          path: changelog/liquibase/select-country.sql
          relativeToChangelogFile: true
          splitStatements: true
          stripComments: true

Create changelog-master-secondary.yaml

Create the below liquibase configuration file – changelog-master-secondary.yaml – under classpath directory src/main/resources/db.

databaseChangeLog:
  - changeSet:
      id: selectRegion
      author: Soumitra
      changes:
      - sqlFile:
          dbms: oracle
          encoding: utf8
          endDelimiter: \n/\s*\n|\n/\s*$
          path: changelog/liquibase/select-region.sql
          relativeToChangelogFile: true
          splitStatements: true
          stripComments: true

Create select-country.sql

Create below select-country.sql file that we want to execute through Liquibase when our application starts up.

Put this file under classpath directory src/main/resources/db/changelog/liquibase.

In your real application, you may have more sql files and more complex sql files.

select 1 from country
/

Create select-region.sql

Create below select-region.sql file that we want to execute through Liquibase when our application starts up.

Put this file under classpath directory src/main/resources/db/changelog/liquibase.

select 1 from region
/

Create Spring Boot Config

Create below Spring configuration class that will provide all configuration beans, such as, for datasource, liquibase etc.

Notice how we define multiple beans for multiple datasources and how we attach separate datasource to separate liquibase configuration.

@Configuration
public class DatabaseConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
	
    @Bean
    @ConfigurationProperties(prefix = "spring.secondaryDatasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
	
    @Bean
    @ConfigurationProperties(prefix = "datasource.primaryLiquibase.liquibase")
    public LiquibaseProperties primaryLiquibaseProperties() {
        return new LiquibaseProperties();
    }
	
    @Bean("liquibase")
    public SpringLiquibase primaryLiquibase() {
        return springLiquibase(primaryDataSource(), primaryLiquibaseProperties());
    }
	
    @Bean
    @ConfigurationProperties(prefix = "datasource.secondaryLiquibase.liquibase")
    public LiquibaseProperties secondaryLiquibaseProperties() {
        return new LiquibaseProperties();
    }
	
    @Bean
    public SpringLiquibase secondaryLiquibase() {
        return springLiquibase(secondaryDataSource(), secondaryLiquibaseProperties());
    }
	
    private static SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperties properties) {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog(properties.getChangeLog());
        liquibase.setContexts(properties.getContexts());
        liquibase.setDefaultSchema(properties.getDefaultSchema());
        liquibase.setDropFirst(properties.isDropFirst());
        liquibase.setShouldRun(properties.isEnabled());
        liquibase.setLabels(properties.getLabels());
        liquibase.setChangeLogParameters(properties.getParameters());
        liquibase.setRollbackFile(properties.getRollbackFile());
        return liquibase;
    }
}

That’s all. Hope you got an idea how to setup Liquibase in Spring for multiple datadources

Related Posts:

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *