数据库连接长时间空闲后,爆连接已经关闭的解决方法

本文介绍了如何通过设置验证查询、测试连接和预测试查询来解决应用启动时出现的连接已关闭错误。通过使用ApacheDBCP和Proxool的数据源配置,确保了连接在返回给用户前的有效性和稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<p style="">在很多场合都碰到有人提问: 第二天第一次访问应用,总是报连接已经关闭(Connection Close)

<p style=""><a style="color: #2970a6; text-decoration: none;" href="http://commons.apache.org/dbcp/api-1.4/org/apache/commons/dbcp/BasicDataSource.html" target="_blank">Apache DBCP</a>:
<div class="wp_codebox_msgheader" style="">
<span class="left" style="padding-bottom: 2px; line-height: 18px; padding-left: 20px; padding-right: 5px; font-family: tahoma, arial, verdana; background-position: 0% 50%; float: left; margin-left: 5px; font-size: 14px; margin-right: 10px; padding-top: 2px;"><a style="color: #2970a6; font-weight: bold; text-decoration: none;">View Code</a><span class="Apple-converted-space"> </span>JAVA</span>
<div class="wp_codebox" style="">
<table style="border-bottom: #cccccc 2px solid; border-left: #cccccc 2px solid; background-color: #ffffff; margin: 5px 0px 10px; width: 586px; border-collapse: collapse; border-top: #cccccc 2px solid; border-right: #cccccc 2px solid;" border="0"><tbody><tr id="p3091">
<td id="p309code1" class="code" style="border-bottom: #cccccc 1px solid; text-align: left; border-left: #cccccc 1px solid; padding-bottom: 3px; padding-left: 10px; padding-right: 10px; vertical-align: top; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 3px;">
dataSource.<span style="color: #006633;">setValidationQuery</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'select 1'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

</td>
</tr></tbody></table>


<div class="wp_codebox_msgheader" style="">
<span class="left" style="padding-bottom: 2px; line-height: 18px; padding-left: 20px; padding-right: 5px; font-family: tahoma, arial, verdana; background-position: 0% 50%; float: left; margin-left: 5px; font-size: 14px; margin-right: 10px; padding-top: 2px;"><a style="color: #2970a6; font-weight: bold; text-decoration: none;">View Code</a><span class="Apple-converted-space"> </span>JAVA</span>
<div class="wp_codebox" style="">
<table style="border-bottom: #cccccc 2px solid; border-left: #cccccc 2px solid; background-color: #ffffff; margin: 5px 0px 10px; width: 586px; border-collapse: collapse; border-top: #cccccc 2px solid; border-right: #cccccc 2px solid;" border="0"><tbody><tr id="p3092">
<td id="p309code2" class="code" style="border-bottom: #cccccc 1px solid; text-align: left; border-left: #cccccc 1px solid; padding-bottom: 3px; padding-left: 10px; padding-right: 10px; vertical-align: top; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 3px;">
dataSource.<span style="color: #006633;">setTestConnectionOnCheckout</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
dataSource.<span style="color: #006633;">setPreferredTestQuery</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'select 1'</span><span style="color: #009900;">)</span>

</td>
</tr></tbody></table>

<p style=""><a style="color: #2970a6; text-decoration: none;" href="http://proxool.sourceforge.net/api-dev/org/logicalcobwebs/proxool/ProxoolDataSource.html" target="_blank">Proxool</a>:
<div class="wp_codebox_msgheader" style="">
<span class="left" style="padding-bottom: 2px; line-height: 18px; padding-left: 20px; padding-right: 5px; font-family: tahoma, arial, verdana; background-position: 0% 50%; float: left; margin-left: 5px; font-size: 14px; margin-right: 10px; padding-top: 2px;"><a style="color: #2970a6; font-weight: bold; text-decoration: none;">View Code</a><span class="Apple-converted-space"> </span>JAVA</span>
<div class="wp_codebox" style="">
<table style="border-bottom: #cccccc 2px solid; border-left: #cccccc 2px solid; background-color: #ffffff; margin: 5px 0px 10px; width: 586px; border-collapse: collapse; border-top: #cccccc 2px solid; border-right: #cccccc 2px solid;" border="0"><tbody><tr id="p3093">
<td id="p309code3" class="code" style="border-bottom: #cccccc 1px solid; text-align: left; border-left: #cccccc 1px solid; padding-bottom: 3px; padding-left: 10px; padding-right: 10px; vertical-align: top; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 3px;">
dataSource.<span style="color: #006633;">setTestBeforeUse</span><span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
dataSource.<span style="color: #006633;">setHouseKeepingTestSql</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'select 1'</span><span style="color: #009900;">)</span>

</td>
</tr></tbody></table>


<p style="">其实原来也就这么简单,返回连接给用户之前,先执行一条简单无害快速的SQL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值