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

mklgxw1f  于 2023-03-08  发布在  Spring
关注(0)|答案(3)|浏览(320)

我想将Hikari池配置为在应用程序启动时立即初始化,而不是在发出第一个查询时。
到目前为止,spring initializer项目显示hikari池是在第一次查询时提供的。
重现问题-创建具有Web、jdbc和mysql依赖项的spring initializr项目。仅在向控制器发出GET请求后创建Hikari池
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.
ix0qys7i

ix0qys7i1#

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

@Component
public class HikariLoader implements ApplicationRunner {

  private final HikariDataSource hikariDataSource;

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

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

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

sycxhyv7

sycxhyv72#

我知道这是一个老问题,但我刚刚遇到了同样的问题。我采取了类似于@Ortomala Lokni的方法-但不是添加一个全新的加载器类,我只是添加了一个@PostConstruct方法到我的控制器,以温暖池作为控制器启动。我认为这是一个更简洁,将完成同样的事情。对于您的例子,它看起来像这样:

@RestController
public class DemoController {

  @Autowired
  private JdbcTemplate template;

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

  @PostConstruct
  private void warmConnectionPool() {
    template.getDataSource().getConnection() //starts the pool
  }
}

我希望有一个光池配置属性,将完成同样的事情,但我无法找到一个!

i34xakig

i34xakig3#

如果加上

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

代替

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

光池的初始化是自动完成的。2我不知道为什么会这样--但是可能在某个类的内部有一些加载器或者其他的东西来做这件事。

相关问题