一尘不染

如何配置hikari池以进行快速初始化?

spring-boot

我想将Hikari池配置为在应用程序启动时急于初始化,而不是在发出第一个查询时初始化。

截至目前,spring initializr项目显示,在第一个查询中已配置了hikari池。

重现问题-创建具有web,jdbc和mysql依赖关系的spring initializr项目。仅在对控制器的GET请求之后创建光池

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

控制者

@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!";
    }

}

在应用程序启动时记录日志

 :: 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)

向控制器发出GET请求后,新的日志

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.

阅读 1421

收藏
2020-05-30

共1个答案

一尘不染

您可以ApplicationLoader在启动时使用并获得连接:

@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();
  }
}

这与问题中给出的代码配合得很好,并已通过Spring Boot 2.1.6进行了测试。

2020-05-30