架构师_程序员_码农网

查看: 65|回复: 0

[资料] Spring Boot 数据库连接池 HikariPool 优化

[复制链接]
发表于 6 天前 | 显示全部楼层
Spring-Boot-2.0.0-M1 版本将默认的数据库连接池从 tomcat jdbc pool 改为了hikari,运行 spring boot 应用的时候,控制台会输出如下:
2021-11-25 14:48:09.429  INFO 22236 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-11-25 14:48:09.813  INFO 22236 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.

Hikari默认配置的默认值如下:
{"connection-timeout":30000,"maximum-pool-size":10,"max-lifetime":1800000,"minimum-idle":10,"validation-timeout":5000,"idle-timeout":600000}

name构造器默认值默认配置validate之后的值validate重置
minIdle-110minIdle<0或者minIdle>maxPoolSize,则被重置为maxPoolSize
maxPoolSize-110如果maxPoolSize小于1,则会被重置。当minIdle<=0被重置为DEFAULT_POOL_SIZE则为10;如果minIdle>0则重置为minIdle的值
maxLifetimeMINUTES.toMillis(30) = 18000001800000如果不等于0且小于30秒则会被重置回30分钟
connectionTimeoutSECONDS.toMillis(30) = 3000030000如果小于250毫秒,则被重置回30秒
validationTimeoutSECONDS.toMillis(5) = 50005000如果小于250毫秒,则会被重置回5秒
loginTimeout1030Math.max(1, (int) MILLISECONDS.toSeconds(500L + connectionTimeout)),为connectionTimeout+500ms转为秒数取整 与 1 取最大者
idleTimeoutMINUTES.toMillis(10) = 600000600000如果idleTimeout+1秒>maxLifetime 且 maxLifetime>0,则会被重置为0;如果idleTimeout!=0且小于10秒,则会被重置为10秒
leakDetectionThreshold00如果大于0且不是单元测试,则进一步判断:(leakDetectionThreshold < SECONDS.toMillis(2) or (leakDetectionThreshold > maxLifetime && maxLifetime > 0),会被重置为0 . 即如果要生效则必须>0,而且不能小于2秒,而且当maxLifetime > 0时不能大于maxLifetime
initializationFailTimeout11-
isAutoCommittruetrue-
isReadOnlyfalsefasle-
isAllowPoolSuspensionfalsefalse-
isIsolateInternalQueriesfalsefalse-
isRegisterMbeansfalsefalse-
sealedfalsetrue运行启动后这个标志为true,表示不再运行修改
poolNamenullHikariPool-1-
catalognullnull-
connectionInitSqlnullnull-
connectionTestQuerynullnull-
dataSourceClassNamenullnull-
schemanullnull-
transactionIsolationNamenullnull-
dataSourcenullnull-
dataSourceProperties{}{}-
threadFactorynullnull-
scheduledExecutornullnull-
metricsTrackerFactorynullnull-
metricRegistrynullnull-
healthCheckRegistrynullnull-
healthCheckProperties{}{}-

在进行压测的时候,应用可能会报错如下:

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)
        at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80)
        at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)
        at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)
        at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
        at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
        at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
        at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
        at com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.intercept(MybatisPlusInterceptor.java:81)
        at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62)
        at com.sun.proxy.$Proxy224.query(Unknown Source)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
        ... 114 more
Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.
        at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:695)
        at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:197)
        at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:162)
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128)
        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
        ... 126 more
QQ截图20211125143733.jpg

错误:Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.

Controller 输出 HikariDataSource 配置接口代码如下:

优化方案,增加最大线程数和增加空闲保持的连接数,修改 application.yml 配置,如下:

(完)




上一篇:POI 中 getLastRowNum() 和 getLastCellNum() 区别
下一篇:【转】测试工具 LoadRunner 和 Jmeter 比较
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2021-12-1 23:51

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表