如何定位 Druid & HikariCP 连接池的连接泄漏问题?
背景最近碰到一个 case,一个 Java 应用无法获取新的数据库连接,日志中出现了以下错误:
com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 5001, active 20, maxActive 20, creating 0<br> at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1894)<br> at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1502)<br> at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1482)<br> at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1463)<br>active 等于 maxActive,说明连接池中的连接已耗尽。
分析报错时间段的数据库连接情况,发现数据库的连接数(Threads_connected)显著增加,但活跃线程数(Threads_running)较低且平稳。活跃线程数低且平稳意味着没有慢查询占用连接。但连接数增加明显,说明连接未被及时释放回连接池。
对于这种在一定时间内没有进行任何操作,但又未及时归还到连接池的连接,其实有个专用名词,即泄漏连接(Leaked Connection)。
下面,我们聊聊泄漏连接的相关问题,包括:
[*]泄漏连接的危害。
[*]泄漏连接的产生原因。
[*]Druid 中如何定位泄漏连接。
[*]HikariCP 中如何定位泄漏连接。
泄漏连接的危害
泄漏连接可能引发以下问题:
[*]连接池耗尽:泄漏的连接会持续占用连接池中的资源,导致可用连接逐渐减少,最终耗尽连接池。
[*]应用性能下降:当连接池中的连接被耗尽时,新的数据库操作无法获取连接,导致请求阻塞或失败,这可能导致应用程序无法正常运行。
[*]数据库资源浪费:泄漏的连接会占用数据库的连接资源,可能导致数据库的连接数达到上限。
[*]连接失效风险:长时间未释放的连接无法通过连接池的 Keep-Alive 机制保持活跃,更容易因空闲超时被 MySQL 服务端或中间件关闭。
当使用这些已关闭的连接执行数据库操作时,会触发经典的 “Communications link failure. The last packet successfully received from the server was xxx milliseconds ago.” 错误。
泄漏连接的产生原因
泄漏连接通常由以下原因导致:
1. 长事务或长连接。
事务长时间未提交或连接长时间未释放。
2. 未关闭连接。
在使用完连接后,未调用close()方法将连接归还到连接池。如,
Connection conn = dataSource.getConnection();<br>// 执行数据库操作<br>// 忘记调用 conn.close();<br>3. 异常未处理。
在数据库操作过程中发生异常,导致连接未正常关闭。如,
Connection conn = null;<br>try {<br> conn = dataSource.getConnection();<br> // 执行数据库操作<br> thrownew RuntimeException("模拟异常");<br>} catch (SQLException e) {<br> e.printStackTrace();<br>} finally {<br> if (conn != null) {<br> try {<br> conn.close(); // 异常发生后,可能不会执行到此处<br> } catch (SQLException e) {<br> e.printStackTrace();<br> }<br> }<br>}<br>Druid 中如何定位泄漏连接
在 Druid 连接池中,可以通过以下参数开启未归还连接的检测:
[*]removeAbandoned:是否回收超时未归还的连接,默认值为 false,表示不回收。
[*]removeAbandonedTimeoutMillis:未归还连接的超时时间(单位:毫秒)。默认值为 300000(即 300 秒)。
[*]logAbandoned:是否将超时未归还的连接信息打印到日志中。默认值为 false,表示不打印。
需要注意的是,logAbandoned 仅在 removeAbandoned 为 true 时生效。也就是说,Druid 连接池不支持仅打印,但不回收超时未归还连接的功能。
实现细节
在从连接池获取连接时,如果removeAbandoned为 true,则会记录连接的堆栈信息和创建时间,用于检测未归还连接。
public DruidPooledConnection getConnectionDirect(long maxWaitMillis) throws SQLException {<br> ...<br> for (; ; ) {<br> DruidPooledConnection poolableConnection;<br> try {<br> poolableConnection = getConnectionInternal(maxWaitMillis);<br> } catch (GetConnectionTimeoutException ex) {<br> ...<br> }<br> ...<br> if (removeAbandoned) {<br> // 记录堆栈信息,方便调试,找出未及时关闭连接的代码位置<br> StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); <br> poolableConnection.connectStackTrace = stackTrace;<br> // 设置连接的connectedTimeNano为当前时间<br> poolableConnection.setConnectedTimeNano();<br> poolableConnection.traceEnable = true;<br> // 将连接加入活跃连接列表,用于后续的未归还连接检测。<br> activeConnectionLock.lock();<br> try {<br> activeConnections.put(poolableConnection, PRESENT);<br> } finally {<br> activeConnectionLock.unlock();<br> }<br> }<br> ...<br> return poolableConnection;<br> }<br> }<br>什么时候会检测连接是否超时呢?
这个实际上是在DestroyConnectionThread的周期任务中进行的,在上一篇文章中,我们提到过DestroyConnectionThread按照一定的时间间隔(由 timeBetweenEvictionRunsMillis 参数决定,默认为 60秒)调用shrink(true, keepAlive)方法,销毁连接池中的过期连接。其实,除了 shrink 方法,它还会调用removeAbandoned()来关闭那些超时未归还的连接。
public class DestroyTask implements Runnable {<br> public DestroyTask() {<br> }<br><br> @Override<br> public void run() {<br> shrink(true, keepAlive);<br><br> if (isRemoveAbandoned()) {<br> removeAbandoned();<br> }<br> }<br><br>}<br>下面,我们看看removeAbandoned()具体的实现细节。
public int removeAbandoned() {<br> int removeCount = 0;<br> // 如果当前没有活跃连接(activeConnections 为空),则直接返回<br> if (activeConnections.size() == 0) {<br> return removeCount;<br> }<br><br> long currrentNanos = System.nanoTime();<br><br> List<DruidPooledConnection> abandonedList = new ArrayList<DruidPooledConnection>();<br><br> activeConnectionLock.lock();<br> try {<br> Iterator<DruidPooledConnection> iter = activeConnections.keySet().iterator();<br> // 遍历活跃连接<br> for (; iter.hasNext(); ) {<br> DruidPooledConnection pooledConnection = iter.next();<br> // 如果连接正在运行(isRunning()),则跳过<br> if (pooledConnection.isRunning()) {<br> continue;<br> }<br> // 计算连接的使用时间(timeMillis),即当前时间减去连接的借出时间。<br> long timeMillis = (currrentNanos - pooledConnection.getConnectedTimeNano()) / (1000 * 1000);<br> // 如果连接的使用时间超过了 removeAbandonedTimeoutMillis,则将其从活跃连接列表中移除,并加入 abandonedList<br> if (timeMillis >= removeAbandonedTimeoutMillis) {<br> iter.remove();<br> pooledConnection.setTraceEnable(false);<br> abandonedList.add(pooledConnection);<br> }<br> }<br> } finally {<br> activeConnectionLock.unlock();<br> }<br> // 遍历 abandonedList,对每个未归还的连接调用 JdbcUtils.close() 关闭连接<br> if (abandonedList.size() > 0) {<br> for (DruidPooledConnection pooledConnection : abandonedList) {<br> ...<br> JdbcUtils.close(pooledConnection);<br> pooledConnection.abandond();<br> removeAbandonedCount++;<br> removeCount++;<br> // 如果 logAbandoned 为 true,则记录未归还连接的详细信息<br> if (isLogAbandoned()) {<br> StringBuilder buf = new StringBuilder();<br> buf.append("abandon connection, owner thread: ");<br> buf.append(pooledConnection.getOwnerThread().getName());<br> buf.append(", connected at : ");<br> ...<br> }<br><br> LOG.error(buf.toString());<br> }<br> }<br> }<br><br> return removeCount;<br>}<br>该方法的处理流程如下:
[*]遍历当前活跃连接(activeConnections),检查每个连接的使用时间。连接的使用时间等于当前时间减去连接的借出时间(即borrow时刻的时间戳)。
[*]如果某个连接的使用时间超过了removeAbandonedTimeoutMillis,则将其加入 abandonedList。
[*]遍历 abandonedList,关闭这些未归还的连接。如果logAbandoned为 true,则会在日志中打印未归还连接的详细信息。通过分析日志,可以定位泄漏连接的代码位置。
HikariCP 中如何定位泄漏连接
在 HikariCP 连接池中,可以通过以下参数开启连接泄漏检测:
[*]leakDetectionThreshold:连接泄漏检测阈值(单位:毫秒)。如果一个连接在从连接池获取后超过指定时间未被关闭,则认为是泄漏连接。默认为 0,表示禁用连接泄漏检测。最小可设置为 2000(2 秒)。
当出现泄漏连接时,HikariCP 日志中会打印以下信息
Connection leak detection triggered for com.mysql.cj.jdbc.ConnectionImpl@5dd31d98 on thread com.example.HikariCPTest.main(), stack trace follows<br>java.lang.Exception: Apparent connection leak detected<br> at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100)<br> at com.example.HikariCPTest.main(HikariCPTest.java:27)<br>...<br>实现细节
在从连接池获取连接后,系统会调用leakTaskFactory.schedule(poolEntry)启动一个 ProxyLeakTask 定时任务。该任务将在leakDetectionThreshold毫秒后触发run()方法,用于检测并打印连接泄漏信息。
public Connection getConnection(final long hardTimeout) throws SQLException<br> {<br> suspendResumeLock.acquire();<br> finalvar startTime = currentTime();<br> try {<br> var timeout = hardTimeout;<br> do {<br> // 从连接池中获取空闲连接<br> var poolEntry = connectionBag.borrow(timeout, MILLISECONDS);<br> if (poolEntry == null) {<br> break; // We timed out... break and throw exception<br> }<br> finalvar now = currentTime();<br> // 若连接已被标记为驱逐 (evict) 或检测到无效,则关闭该连接<br> if (poolEntry.isMarkedEvicted() || (elapsedMillis(poolEntry.lastAccessed, now) > aliveBypassWindowMs && isConnectionDead(poolEntry.connection))) {<br> closeConnection(poolEntry, poolEntry.isMarkedEvicted() ? EVICTED_CONNECTION_MESSAGE : DEAD_CONNECTION_MESSAGE);<br> timeout = hardTimeout - elapsedMillis(startTime);<br> }<br> else {<br> ...<br> // 返回一个代理连接,并启动连接泄漏检测任务<br> return poolEntry.createProxyConnection(leakTaskFactory.schedule(poolEntry));<br> }<br> } while (timeout > 0L);<br> ...<br> }<br>如果连接在leakDetectionThreshold时间内被归还(即调用了close()方法),系统会调用leakTask.cancel()取消定时任务,从而避免触发run()方法。
如果连接超时未归还,系统将执行 run() 方法,打印连接泄漏信息。
以下是 ProxyLeakTask 的具体实现。
class ProxyLeakTask implements Runnable<br>{<br> ...<br> ProxyLeakTask(final PoolEntry poolEntry)<br> {<br> this.exception = new Exception("Apparent connection leak detected");<br> this.threadName = Thread.currentThread().getName();<br> this.connectionName = poolEntry.connection.toString();<br> }<br> ...<br> void schedule(ScheduledExecutorService executorService, long leakDetectionThreshold)<br> {<br> scheduledFuture = executorService.schedule(this, leakDetectionThreshold, TimeUnit.MILLISECONDS);<br> }<br><br> /** {@inheritDoc} */<br> @Override<br> public void run()<br> {<br> isLeaked = true;<br><br> finalvar stackTrace = exception.getStackTrace();<br> finalvar trace = new StackTraceElement;<br><br> System.arraycopy(stackTrace, 5, trace, 0, trace.length);<br><br> exception.setStackTrace(trace);<br> LOGGER.warn("Connection leak detection triggered for {} on thread {}, stack trace follows", connectionName, threadName, exception);<br> }<br><br> void cancel()<br> {<br> scheduledFuture.cancel(false);<br> if (isLeaked) {<br> LOGGER.info("Previously reported leaked connection {} on thread {} was returned to the pool (unleaked)", connectionName, threadName);<br> }<br> }<br>}<br>总结
泄漏连接是指在使用完数据库连接后未及时归还连接池的连接。泄漏连接的主要危害包括连接池耗尽、应用性能下降、数据库资源浪费以及潜在的连接失效风险。泄漏连接的产生原因通常包括未正确关闭连接、未处理异常或长事务等。
Druid 和 HikariCP 两大常用连接池提供了相应的泄漏连接检测机制。Druid 通过DestroyConnectionThread周期性检测未归还的连接,并在超时后关闭这些连接。如果logAbandoned为 true,还会打印未归还连接的详细信息。HikariCP 则通过leakDetectionThreshold参数开启连接泄漏检测。当连接在指定时间内未被归还时,HikariCP 会触发ProxyLeakTask,打印连接泄漏信息。
在开发和测试环境中,建议开启连接泄漏检测功能,以便尽早发现问题并进行修复。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]