gpt4 book ai didi

spring-boot - 如何配置 hikari 池以进行急切初始化?

转载 作者:行者123 更新时间:2023-12-03 14:16:55 32 4
gpt4 key购买 nike

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

截至目前,spring initializr 项目显示在第一次查询时提供了 hikari 池。

重现问题 - 使用 web、jdbc 和 mysql 依赖项创建 spring initializr 项目。 Hikari 池仅在向 Controller 发出 GET 请求后创建

应用程序属性

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

}

应用程序启动时的日志
 :: 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)

向 Controller 发出 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.

最佳答案

您可以使用 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();
}
}
这适用于问题中给出的代码,并使用 Spring Boot 2.1.6 进行了测试。

关于spring-boot - 如何配置 hikari 池以进行急切初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908309/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com