mariosk89
mariosk89

Reputation: 954

Spring Boot - Configure and initialise multiple datasources

I am using Spring Boot and Liquibase to initialise a database for my project. Due to new requirements, I have to split my database tables between two different schemas.

I've managed to configure both datasources for my project using the @Primary annotation but I am wondering if there is a way to initialise the two databases separately, creating different tables for each database.

At the moment I can only get my primary database initialised using the liquibase yaml script that I originally had

Upvotes: 2

Views: 5335

Answers (2)

mariosk89
mariosk89

Reputation: 954

I managed to initialise both databases by using two Liquibase beans. In my Configuration class I have the following

@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "primaryLiquibaseProperties")
@ConfigurationProperties("liquibase-changelogs.primary.liquibase")
public LiquibaseProperties primaryLiquibaseProperties() {
    return new LiquibaseProperties();
}

@Bean(name = "liquibase")
public SpringLiquibase primaryLiquibase(@Qualifier("primaryLiquibaseProperties") LiquibaseProperties liquibaseProperties) {
    SpringLiquibase primary = new SpringLiquibase();
    primary.setDataSource(dataSource());
    primary.setChangeLog(primaryLiquibaseProperties().getChangeLog());

    return primary;
}

@Bean(name = "metadata_datascource")
@ConfigurationProperties("spring.metadata_datascource")
public DataSource metadataDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "metadataLiquibaseProperties")
@ConfigurationProperties("liquibase-changelogs.metadate.liquibase")
public LiquibaseProperties metadataLiquibaseProperties() {
    return new LiquibaseProperties();
}

@Bean(name = "metadata-liquibase")
public SpringLiquibase metadataLiquibase(@Qualifier("metadataLiquibaseProperties") LiquibaseProperties liquibaseProperties) {
    SpringLiquibase metadata = new SpringLiquibase();
    metadata.setDataSource(metadataDataSource());
    metadata.setChangeLog(metadataLiquibaseProperties().getChangeLog());

    return metadata;
}

and in my properties I have:

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: ...
    username: abc
    password: abc
    jpa:
      hibernate:
        ddl-auto: update
  metadata_datascource:
    driver-class-name: org.postgresql.Driver
    url: ...
    username: abc
    password: abc
    jpa:
      hibernate:
        ddl-auto: update

liquibase-changelogs:
  primary:
    liquibase:
        change-log: classpath:db/changelog/primary.yaml
  metadata:
    liquibase:
        change-log: classpath:db/changelog/metadata.yaml

Upvotes: 2

ThomasRS
ThomasRS

Reputation: 8287

Yes, multiple DataSources or any type of bean is possible within Spring. It is just necessary to make sure that Spring is able to identify which instance to autowire where, so basically

  1. use bean id
  2. use @Qualifier when autowiring

Upvotes: 1

Related Questions