Prashanth Sg
Prashanth Sg

Reputation: 3

Spring boot - connection pool properties for more than one DataSource

I'm using spring boot version 1.5.2 and I have a use case where I need to configure 2 data sources in an application. I have been successful in getting the app to connect to 2 databases, however I'm not able to set connection pool properties for them.

Here's the configuration class:

@Configuration
public class DataSourceConfig {

@Bean(name = "oneDataSource")
@ConfigurationProperties(prefix = "spring.datasource.one")
@Primary
public DataSource oneDataSource() {
    return DataSourceBuilder.create().build();        
}

@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.two")
public DataSource twoDataSource() {
    return DataSourceBuilder.create().build();
}
}

On debugging DataSourceBuilder, I can see that it is instantiating beans of type org.apache.tomcat.jdbc.pool.DataSource, which is what I want, however the poolProperties field in the DataSource object always contains some default properties and not what I intended.

Here's my application.yml:

spring:
  profiles: dev

  datasource:
    one:
        url: jdbc:mariadb://localhost:3306/one
        username: user1
        password: password
        driverClassName: org.mariadb.jdbc.Driver
        initialize: true
        tomcat:
            testOnBorrow: true
            validation-query: SELECT 1
            testWhileIdle: true
            continueOnError: true
            initialSize: 2
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 5000
            minIdle: 2
            maxIdle: 3
            maxActive: 30
    two:
        url: jdbc:mariadb://localhost:3306/two
        username: user1
        password: password
        driverClassName: org.mariadb.jdbc.Driver
        initialize: true
        tomcat:
            testOnBorrow: true
            validation-query: SELECT 1
            testWhileIdle: true
            continueOnError: true
            initialSize: 2
            timeBetweenEvictionRunsMillis: 5000
            minEvictableIdleTimeMillis: 5000
            minIdle: 2
            maxIdle: 3
            maxActive: 30

Am I missing something? Is DataSourceBuilder even capable of looking for the pool properties configured this way? What would be the best way to configure 2 data sources with my desired pool properties?

Upvotes: 0

Views: 3702

Answers (1)

Hashcon
Hashcon

Reputation: 597

First, you should ignore DataSourceAutoConfiguration:

@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class
})
public class Main{
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

Then, keep your configuration in your question. If you use mybatis as DAO Framework, you should add these configuration as shown below:

@Configuration
@MapperScan(basePackages = {"mapperpackage1"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {

    @Autowired
    @Qualifier("oneDataSource")
    private DataSource ds1;


    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds1); 

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); 
        return template;
    }
}

And similarly:

@Configuration
@MapperScan(basePackages = {"mapperpackage2"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbAConfig2 {

    @Autowired
    @Qualifier("twoDataSource")
    private DataSource ds2;


    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds2); 

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2()); 
        return template;
    }
}

Upvotes: 2

Related Questions