Multi-tenancy Dynamic switch between Datasources is not working

607 views Asked by At

I am trying to implement multi-tenancy in my spring boot application. I am referring "http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html" example. In this example everything is working fine. but in my code Switching between data source is not happening.

In my DataSourceConfig.java I am facing problem to declare 3 Beans, So i am using @Primary annotation. If I remove @Primary annotation I am getting bellow error

' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] ' 

But if I use @Primary annotation irrespective of tenant id its going and storing the data in DataSource annotated with @Primary. So Switching of DataSource is not happening.

This is my DataSourceConfig class

package com.config;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.netflix.governator.annotations.binding.Secondary;

@Configuration
public class DataSourceConfig {

@Autowired
private MultitenancyProperties multitenancyProperties;

@Primary
@Bean(name =  {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource1().getClassLoader())
            //.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource1().getUsername())
            .password(this.multitenancyProperties.getDatasource1().getPassword())
            .url(this.multitenancyProperties.getDatasource1().getUrl());
    return factory.build();
}


@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
    System.out.println("Entered to 2");
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource2().getClassLoader())
            //.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource2().getUsername())
            .password(this.multitenancyProperties.getDatasource2().getPassword())
            .url(this.multitenancyProperties.getDatasource2().getUrl());
    return factory.build();
}


@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.multitenancyProperties.getDatasource3().getClassLoader())
            //.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
            .username(this.multitenancyProperties.getDatasource3().getUsername())
            .password(this.multitenancyProperties.getDatasource3().getPassword())
            .url(this.multitenancyProperties.getDatasource3().getUrl());
    return factory.build();
}}
1

There are 1 answers

0
Pavan On BEST ANSWER

Disable Spring's DataSourceAutoConfiguration and provide our multi-tenant DataSourceConfig. This will load the properties from MultitenancyProperties which in turn has been configured by application.properties and configure our datasources accordingly. To exclude DataSourceAutoConfiguration use @EnableAutoConfiguration

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})