您的位置:首页 > 数据库

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

2011-12-02 16:41 218 查看
<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 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐