Spring Boot 2 Hikari数据源配置错误?

时间:2019-04-03 17:33:01

标签: spring-boot hikaricp

尝试配置2个Hikari数据源,但只有一个被正确拾取,并且Hikari日志记录显示了配置。除了将其中一个设置为“主要”外,这些属性是相同的。

ActivityConfig:

@Configuration
public class ActivityConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "activity.datasource")
    public DataSourceProperties activityDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "activity-datasource")
    @Primary
    @ConfigurationProperties(prefix = "activity.datasource.hikari")
    public HikariDataSource activityDataSource() {
         System.out.println("properties url = " + activityDataSourceProperties().getUrl());
        HikariDataSource hikariDataSource = activityDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class)
                   .build();
        return hikariDataSource;
    }

SubscriptionConfig:

@Configuration
public class SubscriptionConfig {

    @Bean
    @ConfigurationProperties(prefix = "subscription.datasource")
    public DataSourceProperties subscriptionDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "subscription-datasource")
    @ConfigurationProperties(prefix = "subscription.datasource.hikari")
    public HikariDataSource subscriptionDataSource() {
        System.out.println("properties url = " + subscriptionDataSourceProperties().getUrl());
        HikariDataSource hikariDataSource = subscriptionDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class)
                   .build();
        return hikariDataSource;

    }

application.properties:

#HikariCP  config
activity.datasource.url=jdbc:mysql://.../activity
activity.datasource.username=XXXX
activity.datasource.password=YYYY
activity.datasource.driverClassName=com.mysql.cj.jdbc.Driver
activity.datasource.hikari.connection-timeout=60000
activity.datasource.hikari.maximum-pool-size=10
activity.datasource.hikari.idle-timeout=300000
activity.datasource.hikari.max-lifetime=1800000
activity.datasource.hikari.minimum-idle=10
activity.datasource.hikari.auto-commit=true
activity.datasource.hikari.validation-timeout=15000
activity.datasource.cachePrepStmts=true
activity.datasource.prepStmtCacheSize=250
activity.datasource.prepStmtCacheSqlLimit=2048
activity.datasource.useServerPrepStmts=true
activity.datasource.useLocalSessionState=true
activity.datasource.rewriteBatchedStatements=true
activity.datasource.cacheResultSetMetadata=true
activity.datasource.cacheServerConfiguration=true
activity.datasource.elideSetAutoCommits=true
activity.datasource.maintainTimeStats=false
activity.datasource.hikari.pool-name=ActivityPool

#HikariCP  config
subscription.datasource.url=jdbc:mysql://.../subscription
subscription.datasource.username=XXXX
subscription.datasource.password=YYYY
subscription.datasource.driverClassName=com.mysql.cj.jdbc.Driver
subscription.datasource.hikari.connection-timeout=60000
subscription.datasource.hikari.maximum-pool-size=10
subscription.datasource.hikari.idle-timeout=300000
subscription.datasource.hikari.max-lifetime=1800000
subscription.datasource.hikari.minimum-idle=10
subscription.datasource.hikari.auto-commit=true
subscription.datasource.hikari.validation-timeout=15000
subscription.datasource.cachePrepStmts=true
subscription.datasource.prepStmtCacheSize=250
subscription.datasource.prepStmtCacheSqlLimit=2048
subscription.datasource.useServerPrepStmts=true
subscription.datasource.useLocalSessionState=true
subscription.datasource.rewriteBatchedStatements=true
subscription.datasource.cacheResultSetMetadata=true
subscription.datasource.cacheServerConfiguration=true
subscription.datasource.elideSetAutoCommits=true
subscription.datasource.maintainTimeStats=false
subscription.datasource.hikari.pool-name=SubscriptionPool

一切正常,但是Hikari仅记录“活动”数据源设置。而且在关闭时,没有提及“ SubscriptionPool ”。

仅来自Hikari的日志记录:

09:51:38.422 [main] WARN  com.zaxxer.hikari.HikariConfig - ActivityPool - idleTimeout has been set but has no effect because the pool is operating as a fixed size pool.
09:51:38.422 [main] DEBUG com.zaxxer.hikari.HikariConfig - ActivityPool - configuration:
09:51:38.423 [main] DEBUG com.zaxxer.hikari.HikariConfig - allowPoolSuspension.............false
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - autoCommit......................true
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - catalog.........................none
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionInitSql...............none
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTestQuery.............none
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTimeout...............60000
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSource......................none
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceClassName.............none
09:51:38.424 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceJNDI..................none
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceProperties............{password=<masked>}
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - driverClassName................."com.mysql.cj.jdbc.Driver"
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckProperties...........{}
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckRegistry.............none
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - idleTimeout.....................300000
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - initializationFailTimeout.......1
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - isolateInternalQueries..........false
09:51:38.425 [main] DEBUG com.zaxxer.hikari.HikariConfig - jdbcUrl.........................jdbc:mysql://.../activity
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - leakDetectionThreshold..........0
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - maxLifetime.....................1800000
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - maximumPoolSize.................10
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricRegistry..................none
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricsTrackerFactory...........none
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - minimumIdle.....................10
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - password........................<masked>
09:51:38.431 [main] DEBUG com.zaxxer.hikari.HikariConfig - poolName........................"ActivityPool"
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - readOnly........................false
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - registerMbeans..................false
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutor...............none
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - schema..........................none
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - threadFactory...................internal
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation............default
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - username........................"XXXX"
09:51:38.432 [main] DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout...............15000

可能与之相关的是,我已指定 subscription 模式的Entity对象被报告为 activity 模式的一部分,并说该表不存在( true!),当调用 find 方法时。

我是否配置错误,还是Hikari / Boot问题?

谢谢。

1 个答案:

答案 0 :(得分:0)

因此我发现我需要在每个配置类上添加 @EnableJpaRepositories 。这样,两个数据源都按预期配置。还必须在每个配置类的 entityManagerFactoryBean 方法中使用的每个 DataSource 参数中添加 @Qualifier

@Bean(name = "subscriptionEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder,
                                                                       @Qualifier("subscription-datasource") DataSource dataSource) {
    return builder.dataSource(dataSource)
                   .packages("com.acornmedia.activeusers.subscription.repo")
                   .persistenceUnit("subscription")
                   .build();