How to configure hikari pool for eager initializat

2020-06-21 06:37发布

问题:

I want to configure the Hikari pool to eagerly initialize on application startup and not when first query is issued.

As of now spring initializr project shows that hikari pool is provisioned on first query.

To reproduce issue - Create spring initializr project with web, jdbc and mysql dependency. Hikari pool is only created after GET request to controller

application.properties

spring.datasource.url=
spring.datasource.driver-class-name=
spring.datasource.username=
spring.datasource.password=
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.connection-init-sql=SELECT 1

Controller

@RestController
public class DemoController {

    @Autowired
    private JdbcTemplate template;

    @GetMapping(value="/request")
    public String testHikariEagerInitialization() {

        template.execute("SELECT COUNT(1) FROM trade");

        return "Hikari Pool created now!";
    }

}

logs at application startup

 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-09 08:36:02.074  INFO 37884 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on  with PID 37884 ()
2019-07-09 08:36:02.078  INFO 37884 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2019-07-09 08:36:03.132  INFO 37884 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-07-09 08:36:03.180  INFO 37884 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-07-09 08:36:03.197  INFO 37884 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-09 08:36:03.330  INFO 37884 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-07-09 08:36:03.330  INFO 37884 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1187 ms
2019-07-09 08:36:03.416 DEBUG 37884 --- [           main] com.zaxxer.hikari.HikariConfig           : Driver class com.mysql.jdbc.Driver found in Thread context class loader sun.misc.Launcher$AppClassLoader@764c12b6
2019-07-09 08:36:03.598  INFO 37884 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-09 08:36:03.832  INFO 37884 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-09 08:36:03.836  INFO 37884 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.168 seconds (JVM running for 2.99)

new logs once GET request is made to controller

2019-07-09 08:39:20.039  INFO 37884 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-07-09 08:39:20.039  INFO 37884 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-07-09 08:39:20.046  INFO 37884 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms
2019-07-09 08:39:20.071 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : demo-memsql-connection-pool - configuration:
2019-07-09 08:39:20.073 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : allowPoolSuspension.............false
2019-07-09 08:39:20.073 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : autoCommit......................true
2019-07-09 08:39:20.073 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : catalog.........................none
2019-07-09 08:39:20.073 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : connectionInitSql..............."SELECT 1"
2019-07-09 08:39:20.073 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : connectionTestQuery.............none
2019-07-09 08:39:20.073 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : connectionTimeout...............30000
2019-07-09 08:39:20.074 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : dataSource......................none
2019-07-09 08:39:20.074 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : dataSourceClassName.............none
2019-07-09 08:39:20.074 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : dataSourceJNDI..................none
2019-07-09 08:39:20.074 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : dataSourceProperties............{password=<masked>}
2019-07-09 08:39:20.075 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : driverClassName................."com.mysql.jdbc.Driver"
2019-07-09 08:39:20.075 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : healthCheckProperties...........{}
2019-07-09 08:39:20.075 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : healthCheckRegistry.............none
2019-07-09 08:39:20.075 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : idleTimeout.....................600000
2019-07-09 08:39:20.075 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : initializationFailTimeout.......1
2019-07-09 08:39:20.075 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : isolateInternalQueries..........false
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : jdbcUrl.........................jdbc:mysql://
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : leakDetectionThreshold..........0
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : maxLifetime.....................1800000
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : maximumPoolSize.................100
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : metricRegistry..................none
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : metricsTrackerFactory...........none
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : minimumIdle.....................10
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : password........................<masked>
2019-07-09 08:39:20.076 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : poolName........................"demo-memsql-connection-pool"
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : readOnly........................false
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : registerMbeans..................true
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : scheduledExecutor...............none
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : schema..........................none
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : threadFactory...................internal
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : transactionIsolation............default
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : username........................""
2019-07-09 08:39:20.077 DEBUG 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig           : validationTimeout...............5000
2019-07-09 08:39:20.077  INFO 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : demo-memsql-connection-pool - Starting...
2019-07-09 08:39:20.466  INFO 37884 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : demo-memsql-connection-pool - Start completed.

回答1:

You can use an ApplicationLoader and get a connection at startup:

@Component
public class HikariLoader implements ApplicationRunner {

  private final HikariDataSource hikariDataSource;

  @Autowired
  public HikariLoader(HikariDataSource hikariDataSource) {
    this.hikariDataSource = hikariDataSource;
  }

  @Autowired
  public void run(ApplicationArguments args) throws SQLException {
    hikariDataSource.getConnection();
  }
}

This works well with the code given in the question and was tested with Spring Boot 2.1.6.