Spring Boor version: 2.2.2.RELEASE

HikariCP version: 3.4.1

Now, I only initialize the database connection when the application is accessed. If the database connection has errors, I can't find the problem quickly. I tried some methods and still can't solve it. E.g

    @Bean
    public LazyInitializationExcludeFilter excludeFilter() {
        return LazyInitializationExcludeFilter.forBeanTypes(HikariDataSource.class);
    }

E.g

spring:
  main:
    lazy-initialization: false

I have two apps with this problem now. One of them runs on JDK8 and the other runs on JDK13.

Comment From: MrXionGe

This is part of the configuration file

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/demos?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: *****************
    hikari:
      pool-name: HikariCP
      minimum-idle: 4
      maximum-pool-size: 16
      connection-timeout: 10000
      idle-timeout: 300000
      max-lifetime: 600000
      connection-init-sql: SET NAMES utf8mb4

Comment From: MrXionGe

I try to switch to another version (2.2.0/2.2.1). This problem also occurs. Maybe the reason for lazy loading.

Comment From: snicoll

@MrXionGe thanks for the report but I am afraid we can't help you with partial code snippet and no stacktrace. Can you please provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem?

Comment From: MrXionGe

@snicoll https://gitee.com/MrXionGe/SimpleWeb.git Compile and run directly. And it will not try to connect to MySQL. Normally, when the application is started, we can see the log like "HikariCP Starting...". But this won't print now. Now, we can only connect to MySQL when the application is accessed and the SQL logic code is triggered for the first time. In this example, there is no configuration for lazy loading. I added it yesterday and it didn't work, so I deleted it.

Comment From: snicoll

@MrXionGe sorry but your complete application does not make a minimal sample. If the problem is with Hikari and MySQL, a small sample should only use that (and your pom should only have dependency to those and remove everything that's unrelated).

I am not sure I understand the problem either. It looks like you're using lazy initialization and you report that the database does not initialize on startup but only when used (which is working as designed and exactly the purpose of lazy initialization). I don't see any use of lazy initialization in your sample so I am confused.

Can you please share a small sample with only what's necessary to reproduce the problem please? Step by step instructions would be appreciated as well.

Comment From: MrXionGe

https://www.mrxionge.cn/file/lazydemo.zip (File has been deleted)

I created a simple application.

I try to turn off lazy initialization. Doesn't seem to work.

May be my configuration is wrong.

If you can help me, that would be great.

Comment From: MrXionGe

@snicoll I uploaded a new example. Hard viewing.

Comment From: snicoll

@MrXionGe thanks for the feedback and the sample but I don't understand what you mean by

I try to turn off lazy initialization. Doesn't seem to work.

Lazy initialization is off by default so the following is useless:

spring:
  main:
    lazy-initialization: false

This can be removed as this is the default behaviour. Can you please describe the actual problem (or how I can reproduce that with your sample) and what you're trying to achieve.

Comment From: MrXionGe

I hope that the ‘HikariDataSource’ can be initialized automatically when the application starts. But now it cannot be initialized automatically. As for lazy initialization, I also tried:

spring:
  main:
    lazy-initialization: off

However, 'HikariDataSource' still cannot be initialized automatically. And I also tried creating the bean:

    @Bean
    public LazyInitializationExcludeFilter excludeFilter() {
        return LazyInitializationExcludeFilter.forBeanTypes(HikariDataSource.class);
    }

It doesn't work either. Normally, ‘HikariDataSource’ is initialized automatically after the application is launched. And we can see ‘HikariCP Starting ...’ on the console.

Comment From: snicoll

Thanks for the update, I am with you now. I got a little bit confused as I thought this was happening by default as well.

Let's take a step back. This problem has nothing to do with lazy initialization. As I've indicated above, this feature is disabled by default. What's happening is that the connection pool for the DataSource (Hikari) is created but nothing is requiring a Connection. The connection pool does not attempt to reach the database until a connection is requested.

Adding the following bean is enough to force validation on startup:

@Bean
public ApplicationRunner validateDataSource(DataSource dataSource) {
    return args -> {
        try (Connection connection = dataSource.getConnection()) {
        }
    };
}

It looks like your other projects are not affected and the Hikari pool is starting when the application starts. Can you check the differences with the samples you've attached?

Also flagging for team attention as I don't know what the next steps for this one should be.

Comment From: wilkinsona

The pool can be created up front by calling HikariDataSource(HikariConfig) rather than HikariDataSource(). That change is being tracked by https://github.com/spring-projects/spring-boot/issues/12186.

Comment From: MrXionGe

@wilkinsona Thank you, I will try it later.

Comment From: MrXionGe

@snicoll Thank you for the example.

    @Bean
    public ApplicationRunner runner(DataSource dataSource) {
        return args -> {
            log.info("dataSource: {}", dataSource);
            Connection connection = dataSource.getConnection();
            log.info("connection: {}", connection);
        };
    }

I added this code. When this code is run, a connection pool is created. It's really fun. springboot HikariDataSource cannot be initialized at application startup

Comment From: MrXionGe

@wilkinsona Thank you. I tried the way you said. When the application starts, a connection pool is created. Code example:

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://xxx.xxx.xxx.xxx:3306/demos?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai");
        config.setUsername("root");
        config.setPassword("xxxxxxxxx");
        config.setXxx("xxx");
        config.setYyy("yyy");
        config.setZzz("zzz");
        return new HikariDataSource(config);
    }