MySQL 的默认设置下,当一个连接的空闲时间超过8小时后,MySQL 就会断开该连接,而 c3p0连接池则以为该被断开的连接依然有效。在这种情况下,如果客户端代码向 c3p0连接池请求连接的话,连接池就会把已经失效的连接返回给客户端,客户端在使用该失效连接的时候即抛出异常。
解决这个问题的办法有三种:
1. 增加 MySQL 的 wait_timeout 属性的值。
修改<wbr><span style="font-style: italic;">/etc/mysql/my.cnf</span><wbr>文件,在[mysqld] 节中设置:<br><div style="border-top-width: 0.5pt; border-right-width: 0.5pt; border-bottom-width: 0.5pt; border-left-width: 0.5pt; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-image: initial; padding-top: 4px; padding-right: 5.4pt; padding-bottom: 4px; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(230, 230, 230); width: 659px; background-position: 0% 50%; background-repeat: repeat repeat;"><div><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><span style="color: rgb(0, 0, 0);">#Set a connection to wait<wbr></wbr></span><span style="color: rgb(0, 0, 0);">8</span><wbr><span style="color: rgb(0, 0, 0);">hours in idle status.<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr>wait_timeout<wbr></wbr></wbr></span><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">86400</span></wbr></wbr></wbr></div></div>2. 减少连接池内连接的生存周期,使之小于上一项中所设置的 wait_timeout 的值。<br>修改 c3p0 的配置文件,设置:<br><div style="border-top-width: 0.5pt; border-right-width: 0.5pt; border-bottom-width: 0.5pt; border-left-width: 0.5pt; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-image: initial; padding-top: 4px; padding-right: 5.4pt; padding-bottom: 4px; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(230, 230, 230); width: 659px; background-position: 0% 50%; background-repeat: repeat repeat;"><div><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><span style="color: rgb(0, 0, 0);">#How long to keep unused connections around(in seconds)<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr># Note: MySQL times out idle connectionsafter<wbr></wbr></wbr></span><span style="color: rgb(0, 0, 0);">8</span><wbr><span style="color: rgb(0, 0, 0);">hours(</span><wbr><span style="color: rgb(0, 0, 0);">28</span><wbr><span style="color: rgb(0, 0, 0);">,</span><wbr><span style="color: rgb(0, 0, 0);">800</span><wbr><span style="color: rgb(0, 0, 0);">seconds)<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr># so ensure this value is below MySQL idletimeout<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr>cpool.maxIdleTime</wbr></wbr></span><wbr><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">25200</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></div></div>在 Spring 的配置文件中:<br><div style="border-top-width: 0.5pt; border-right-width: 0.5pt; border-bottom-width: 0.5pt; border-left-width: 0.5pt; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-image: initial; padding-top: 4px; padding-right: 5.4pt; padding-bottom: 4px; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(230, 230, 230); width: 659px; background-position: 0% 50%; background-repeat: repeat repeat;"><div><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><span style="color: rgb(0, 0, 255);"><</span><wbr><span style="color: rgb(128, 0, 0);">bean<wbr></wbr></span><span style="color: rgb(255, 0, 0);">id</span><wbr><span style="color: rgb(0, 0, 255);">="dataSource"</span><wbr><span style="color: rgb(255, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr>class</wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 0, 255);">="com.mchange.v2.c3p0.ComboPooledDataSource"</span><wbr><span style="color: rgb(0, 0, 255);">></span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 0, 255);"><</span><wbr><span style="color: rgb(128, 0, 0);">property<wbr></wbr></span><span style="color: rgb(255, 0, 0);">name</span><wbr><span style="color: rgb(0, 0, 255);">="maxIdleTime"</span><wbr><span style="color: rgb(255, 0, 0);">value</span><wbr><span style="color: rgb(0, 0, 255);">="${cpool.maxIdleTime}"</span><wbr><span style="color: rgb(0, 0, 255);">/></span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 128, 0);"><!--</span><wbr><span style="color: rgb(0, 128, 0);">otherproperties<wbr></wbr></span><span style="color: rgb(0, 128, 0);">--></span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr></wbr></span><span style="color: rgb(0, 0, 255);"></</span><wbr><span style="color: rgb(128, 0, 0);">bean</span><wbr><span style="color: rgb(0, 0, 255);">></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></div></div>3. 定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。<br>修改 c3p0 的配置文件,设置:<br><div style="border-top-width: 0.5pt; border-right-width: 0.5pt; border-bottom-width: 0.5pt; border-left-width: 0.5pt; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-image: initial; padding-top: 4px; padding-right: 5.4pt; padding-bottom: 4px; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(230, 230, 230); width: 659px; background-position: 0% 50%; background-repeat: repeat repeat;"><div><pre style="white-space: pre-wrap; word-wrap: break-word;"><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><span style="color: rgb(0, 0, 0);"># Prevent MySQL raise exception after a long idle time<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法">cpool.preferredTestQuery</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">'SELECT </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">'<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法">cpool.idleConnectionTestPeriod<wbr></wbr></span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">18000</span><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法">cpool.testConnectionOnCheckout<wbr></wbr></span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">true</span></pre></div></div>修改 Spring 的配置文件:<br><div style="border-top-width: 0.5pt; border-right-width: 0.5pt; border-bottom-width: 0.5pt; border-left-width: 0.5pt; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-image: initial; padding-top: 4px; padding-right: 5.4pt; padding-bottom: 4px; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(230, 230, 230); width: 659px; background-position: 0% 50%; background-repeat: repeat repeat;"><div><pre style="white-space: pre-wrap; word-wrap: break-word;"><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">bean </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="dataSource"</span><span style="color: rgb(255, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr> class</wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);">="com.mchange.v2.c3p0.ComboPooledDataSource"</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="preferredTestQuery"</span><span style="color: rgb(255, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> value</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);">="${cpool.preferredTestQuery}"</span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="idleConnectionTestPeriod<wbr>"</wbr></span><span style="color: rgb(255, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> value</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);">="${cpool.idleConnectionTestPeriod<wbr>}"</wbr></span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">property </span><span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">="testConnectionOnCheckout<wbr>"</wbr></span><span style="color: rgb(255, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> value</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);">="${cpool.testConnectionOnCheckout<wbr>}"</wbr></span><span style="color: rgb(255, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">/></span><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 128, 0);"><!--</span><span style="color: rgb(0, 128, 0);"> other properties </span><span style="color: rgb(0, 128, 0);">--></span><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"></span><span style="color: rgb(0, 0, 255);"></</span><span style="color: rgb(128, 0, 0);">bean</span><span style="color: rgb(0, 0, 255);">></span><span style="color: rgb(0, 0, 0);"> <br></span></pre></div></div><br>附:以下 awk 脚本可以用以将 c3p0.properties 文件中的属性设置转换成为applicationContext.xml 中 数据库连接池 DataSource 所需的 XML 元素形式。<br><div style="border-top-width: 0.5pt; border-right-width: 0.5pt; border-bottom-width: 0.5pt; border-left-width: 0.5pt; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-image: initial; padding-top: 4px; padding-right: 5.4pt; padding-bottom: 4px; padding-left: 5.4pt; background-image: none; background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: rgb(230, 230, 230); width: 659px; background-position: 0% 50%; background-repeat: repeat repeat;"><div><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><span style="color: rgb(0, 0, 0);">#</span><wbr><span style="color: rgb(0, 0, 0);">!</span><wbr><span style="color: rgb(0, 0, 0);">/</span><wbr><span style="color: rgb(0, 0, 0);">bin</span><wbr><span style="color: rgb(0, 0, 0);">/</span><wbr><span style="color: rgb(0, 0, 0);">awk<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr>BEGIN {<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr>FS</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">"</span><wbr><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">"</span><wbr><span style="color: rgb(0, 0, 0);">;</span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr>}<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr>{<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 0, 255);">if</span><wbr><span style="color: rgb(0, 0, 0);">(</span><wbr><span style="color: rgb(0, 0, 0);">NF<wbr></wbr></span><span style="color: rgb(0, 0, 0);">==</span><wbr><span style="color: rgb(0, 0, 0);">2</span><wbr><span style="color: rgb(0, 0, 0);">)</span><wbr><span style="color: rgb(0, 0, 0);">{<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 0, 255);">if</span><wbr><span style="color: rgb(0, 0, 0);">((</span><wbr><span style="color: rgb(0, 0, 0);">x</span><wbr><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">index</span><wbr><span style="color: rgb(0, 0, 0);">($</span><wbr><span style="color: rgb(0, 0, 0);">1</span><wbr><span style="color: rgb(0, 0, 0);">,</span><wbr><span style="color: rgb(0, 0, 0);">"</span><wbr><span style="color: rgb(0, 0, 0);">.</span><wbr><span style="color: rgb(0, 0, 0);">"</span><wbr><span style="color: rgb(0, 0, 0);">))</span><wbr><span style="color: rgb(0, 0, 0);">></span><wbr><span style="color: rgb(0, 0, 0);">0</span><wbr><span style="color: rgb(0, 0, 0);">)</span><wbr><span style="color: rgb(0, 0, 0);">{<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>property_name<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">substr</span><wbr><span style="color: rgb(0, 0, 0);">($</span><wbr><span style="color: rgb(0, 0, 0);">1</span><wbr><span style="color: rgb(0, 0, 0);">,</span><wbr><span style="color: rgb(0, 0, 0);">x</span><wbr><span style="color: rgb(0, 0, 0);">+</span><wbr><span style="color: rgb(0, 0, 0);">1</span><wbr><span style="color: rgb(0, 0, 0);">,</span><wbr><span style="color: rgb(0, 0, 0);">length</span><wbr><span style="color: rgb(0, 0, 0);">($</span><wbr><span style="color: rgb(0, 0, 0);">1</span><wbr><span style="color: rgb(0, 0, 0);">));</span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 255);">else</span><wbr><span style="color: rgb(0, 0, 0);">{<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>property_name<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="color: rgb(0, 0, 0);">=</span><wbr><span style="color: rgb(0, 0, 0);">$</span><wbr><span style="color: rgb(0, 0, 0);">1</span><wbr><span style="color: rgb(0, 0, 0);">;</span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>printf</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><span style="color: rgb(0, 0, 0);">(</span><wbr><span style="color: rgb(0, 0, 0);">"</span><wbr><span style="color: rgb(0, 0, 0);"><property name="%s"value="${%s}"/><wbr></wbr></span><span style="color: rgb(0, 0, 0);">"</span><wbr><span style="color: rgb(0, 0, 0);">,</span><wbr><span style="color: rgb(0, 0, 0);">property_name</span><wbr><span style="color: rgb(0, 0, 0);">,</span><wbr><span style="color: rgb(0, 0, 0);">$</span><wbr><span style="color: rgb(0, 0, 0);">1</span><wbr><span style="color: rgb(0, 0, 0);">);</span><wbr><span style="color: rgb(0, 0, 0);"><br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr><wbr><wbr><wbr><wbr>}<br><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial;" title="mysql<wbr>8小时连接池异常和解决方法"><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></div></div></wbr></wbr>
对MySQL server hasgone away错误最常见的原因是服务器超时了并且关闭了连接。缺省地,如果没有事情发生,服务器在8个小时后关闭连接。可在启动mysqld时通过设置wait_timeout变量改变时间限制。
<wbr><wbr><wbr><wbr><wbr><wbr>具体操作如下:<br><wbr><wbr><wbr><wbr><wbr><wbr>在MS-DOC 中 MySql 5.0/bin 目录下运行 :mysqld --verbose--help<wbr><wbr>,得到 MySql 的参数配置。从中看到wait_timeout = 28800 ,也就是8小时。如果超过 8 小时,没有对数据库进行操作,数据连接就会自动断开。<br><br><wbr><wbr><wbr><wbr>解决方法一:<br><wbr><wbr><wbr><wbr>可以在 my.ini文件中启动项中添加<wbr><wbr>wait_timeout = 86400任意值来设置等待时间。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
|