How to use HikariCP in Spring Boot with two datasources in conjunction with Flyway

后端 未结 2 912
陌清茗
陌清茗 2021-02-03 23:50

I want to use HikariCP as JDBC connection pool in my Spring boot application. I have two datasources (MySQL database as the primary database and accessing those data through Hib

2条回答
  •  渐次进展
    2021-02-04 00:33

    Declaring your own DataSource will already have implicity disabled Spring Boot's auto-configuration of a data source. In other words this won't be having any effect:

    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
    

    I think the problem lies in the fact that you aren't binding Hikari-specific configuration to your MySQL DataSource. You need to do something like this:

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.hikari")
    public DataSource mySQLDataSource() {
        return mySQLDataSourceProperties().initializeDataSourceBuilder().build();
    }
    

    This will mean that your mySQLDataSourceProperties are configured with general-purpose data source configuration. They then create a HikariDataSource which is further configured with the Hikari-specific configuration.

提交回复
热议问题