Hikari数据库连接池泄露问题排查
1、配置文件中配置泄露监测阈值
spring.datasource.hikari.leak-detection-threshold=600000
2、log配置日志输出
<Configuration status="INFO" monitorInterval="5" packages="com.jiuqi.np.log.log4j2.appender"> <properties><!--设置日志在硬盘上输出的目录--><!-- System.out.println(System.getProperty("java.io.tmpdir"));C:\Users\Kim\AppData\Local\Temp--><property name="logPath">${sys:java.io.tmpdir}/jiuqi/logs</property><property name="historyLogPath">${sys:java.io.tmpdir}/jiuqi/logs/history</property></properties><Appenders><RollingFile name="hikariLogFile" fileName="${logPath}/hikari.log" filePattern="${historyLogPath}/$${date:yyyy-MM}/hikari-%d{yyyy-MM-dd}-%i.log.zip"><PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} [%t] %C:%M:%L - %msg%n" /><Policies><TimeBasedTriggeringPolicy interval="1" modulate="true" /><SizeBasedTriggeringPolicy size="20M" /></Policies><DefaultRolloverStrategy max="20" /></RollingFile></Appenders><Loggers><Logger name="com.zaxxer.hikari" level="INFO" additivity="false"><appender-ref ref="hikariLogFile" /></Logger></Loggers></Configuration>
3、查看日志输出
win系统上述配置的默认文件夹地址:C:\Users\JIUQI\AppData\Local\Temp\jiuqi\logs
也可以在启动参数配置:java -Djava.io.tmpdir=/path/to/tmpdir
-08-22 12:43:38.819 [nr housekeeper] com.zaxxer.hikari.pool.ProxyLeakTask:run:84 - Connection leak detection triggered for oracle.jdbc.driver.T4CConnection@ac2d942 on thread http-nio-8188-exec-7, stack trace followsjava.lang.Exception: Apparent connection leak detectedat com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-4.0.3.jar:?]at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:159) ~[spring-jdbc-5.3.20.jar:5.3.20]at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:117) ~[spring-jdbc-5.3.20.jar:5.3.20]at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-5.3.20.jar:5.3.20]at com.jiuqi.nr.dataentry.web.ActionController.batchCheckForm(ActionController.java:823) ~[main/:?]at com.jiuqi.nr.dataentry.web.ActionController$$FastClassBySpringCGLIB$$1.invoke(<generated>) ~[main/:?]
注意:有些数据库驱动,在远程连接数据库时hakari.log日志会抛出异常
-08-22 12:24:39.994 [main] com.zaxxer.hikari.pool.PoolBase:getAndSetNetworkTimeout:541 - nr - Driver does not support get/set network timeout for connections. (oracle.jdbc.driver.T4CConnection.getNetworkTimeout()I)
这种大概率时数据库驱动问题,如:上述问题用的ojdbc6,换成ojdbc7就没问题了。