﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-少年阿宾-随笔分类-dbConnectPool</title><link>http://www.blogjava.net/stevenjohn/category/54640.html</link><description>那些青春的岁月</description><language>zh-cn</language><lastBuildDate>Mon, 15 Dec 2014 12:53:45 GMT</lastBuildDate><pubDate>Mon, 15 Dec 2014 12:53:45 GMT</pubDate><ttl>60</ttl><item><title> 解决 c3p0 和 MySQL 集成情况下，连接长时间闲置后重新使用时报错的问题</title><link>http://www.blogjava.net/stevenjohn/archive/2014/12/15/421421.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Mon, 15 Dec 2014 12:15:00 GMT</pubDate><guid>http://www.blogjava.net/stevenjohn/archive/2014/12/15/421421.html</guid><wfw:comment>http://www.blogjava.net/stevenjohn/comments/421421.html</wfw:comment><comments>http://www.blogjava.net/stevenjohn/archive/2014/12/15/421421.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stevenjohn/comments/commentRss/421421.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stevenjohn/services/trackbacks/421421.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">MySQL 的默认设置下，当一个连接的空闲时间超过8小时后，MySQL 就会断开该连接，而 c3p0 连接池则以为该被断开的连接依然有效。在这种情况下，如果客户端代码向 c3p0 连接池请求连接的话，连接池就会把已经失效的连接返回给客户端，客户端在使用该失效连接的时候即抛出异常。</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">解决这个问题的办法有三种：</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">1. 增加 MySQL 的 wait_timeout 属性的值。</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">修改&nbsp;</span><span style="color: #333333; font-family: Arial; line-height: 26px; font-style: italic; background-color: #ffffff;">/etc/mysql/my.cnf</span><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">&nbsp;文件，在 [mysqld] 节中设置：</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><div style="color: #333333; font-family: Arial; line-height: 26px; border: 0.5pt solid windowtext; padding: 4px 5.4pt; width: 672.59375px; background-image: none; background-attachment: scroll; background-color: #e6e6e6; background-position: 0% 50%; background-repeat: repeat repeat;"><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><span style="color: #000000;">#&nbsp;Set&nbsp;a&nbsp;connection&nbsp;to&nbsp;wait&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;hours&nbsp;in&nbsp;idle&nbsp;status.<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />wait_timeout&nbsp;</span><span style="color: #000000;">=</span>&nbsp;<span style="color: #000000;">86400</span></div><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">2. 减少连接池内连接的生存周期，使之小于上一项中所设置的 wait_timeout 的值。</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">修改 c3p0 的配置文件，设置：</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><div style="color: #333333; font-family: Arial; line-height: 26px; border: 0.5pt solid windowtext; padding: 4px 5.4pt; width: 672.59375px; background-image: none; background-attachment: scroll; background-color: #e6e6e6; background-position: 0% 50%; background-repeat: repeat repeat;"><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><span style="color: #000000;">#&nbsp;How&nbsp;long&nbsp;to&nbsp;keep&nbsp;unused&nbsp;connections&nbsp;around(in&nbsp;seconds)<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />#&nbsp;Note:&nbsp;MySQL&nbsp;times&nbsp;out&nbsp;idle&nbsp;connections&nbsp;after&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">&nbsp;hours(</span><span style="color: #000000;">28</span><span style="color: #000000;">,</span><span style="color: #000000;">800</span><span style="color: #000000;">&nbsp;seconds)<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />#&nbsp;so&nbsp;ensure&nbsp;this&nbsp;value&nbsp;is&nbsp;below&nbsp;MySQL&nbsp;idle&nbsp;timeout<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />cpool.maxIdleTime</span><span style="color: #000000;">=</span><span style="color: #000000;">25200</span></div><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">在 Spring 的配置文件中：</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><div style="color: #333333; font-family: Arial; line-height: 26px; border: 0.5pt solid windowtext; padding: 4px 5.4pt; width: 672.59375px; background-image: none; background-attachment: scroll; background-color: #e6e6e6; background-position: 0% 50%; background-repeat: repeat repeat;"><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean&nbsp;</span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="dataSource"</span><span style="color: #ff0000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;class</span><span style="color: #0000ff;">="com.mchange.v2.c3p0.ComboPooledDataSource"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="maxIdleTime"</span><span style="color: #ff0000;">&nbsp;value</span><span style="color: #0000ff;">="${cpool.maxIdleTime}"</span>&nbsp;<span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">&nbsp;other&nbsp;properties&nbsp;</span><span style="color: #008000;">--&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /></span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span></div><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">3. 定期使用连接池内的连接，使得它们不会因为闲置超时而被 MySQL 断开。</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">修改 c3p0 的配置文件，设置：</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><div style="color: #333333; font-family: Arial; line-height: 26px; border: 0.5pt solid windowtext; padding: 4px 5.4pt; width: 672.59375px; background-image: none; background-attachment: scroll; background-color: #e6e6e6; background-position: 0% 50%; background-repeat: repeat repeat;"><pre style="white-space: pre-wrap; word-wrap: break-word;"><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><span style="color: #000000;">#&nbsp;Prevent&nbsp;MySQL&nbsp;raise&nbsp;exception&nbsp;after&nbsp;a&nbsp;long&nbsp;idle&nbsp;time<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />cpool.preferredTestQuery</span><span style="color: #000000;">=</span><span style="color: #000000;">'SELECT&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">'<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />cpool.idleConnectionTestPeriod</span><span style="color: #000000;">=</span><span style="color: #000000;">18000</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />cpool.testConnectionOnCheckout</span><span style="color: #000000;">=</span><span style="color: #000000;">true</span></pre></div><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">修改 Spring 的配置文件：</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><div style="color: #333333; font-family: Arial; line-height: 26px; border: 0.5pt solid windowtext; padding: 4px 5.4pt; width: 672.59375px; background-image: none; background-attachment: scroll; background-color: #e6e6e6; background-position: 0% 50%; background-repeat: repeat repeat;"><pre style="white-space: pre-wrap; word-wrap: break-word;"><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean&nbsp;</span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="dataSource"</span><span style="color: #ff0000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;class</span><span style="color: #0000ff;">="com.mchange.v2.c3p0.ComboPooledDataSource"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="preferredTestQuery"</span><span style="color: #ff0000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="color: #0000ff;">="${cpool.preferredTestQuery}"</span>&nbsp;<span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="idleConnectionTestPeriod"</span><span style="color: #ff0000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="color: #0000ff;">="${cpool.idleConnectionTestPeriod}"</span>&nbsp;<span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property&nbsp;</span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="testConnectionOnCheckout"</span><span style="color: #ff0000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value</span><span style="color: #0000ff;">="${cpool.testConnectionOnCheckout}"</span>&nbsp;<span style="color: #0000ff;">/&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">&nbsp;other&nbsp;properties&nbsp;</span><span style="color: #008000;">--&gt;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /></span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;"> <br /></span></pre></div><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">附：以下 awk 脚本可以用以将 c3p0.properties 文件中的属性设置转换成为 applicationContext.xml 中 数据库连接池 DataSource 所需的 XML 元素形式。</span><br style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;" /><div style="color: #333333; font-family: Arial; line-height: 26px; border: 0.5pt solid windowtext; padding: 4px 5.4pt; width: 672.59375px; background-image: none; background-attachment: scroll; background-color: #e6e6e6; background-position: 0% 50%; background-repeat: repeat repeat;"><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><span style="color: #000000;">#</span><span style="color: #000000;">!</span><span style="color: #000000;">/</span><span style="color: #000000;">bin</span><span style="color: #000000;">/</span><span style="color: #000000;">awk<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" /><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />BEGIN&nbsp;{<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;FS</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />}<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />{<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span>&nbsp;<span style="color: #000000;">(</span><span style="color: #000000;">NF&nbsp;</span><span style="color: #000000;">==</span>&nbsp;<span style="color: #000000;">2</span><span style="color: #000000;">)</span><span style="color: #000000;">&nbsp;{<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span>&nbsp;<span style="color: #000000;">((</span><span style="color: #000000;">x</span><span style="color: #000000;">=</span><span style="color: #000000;">index</span><span style="color: #000000;">($</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span>&nbsp;<span style="color: #000000;">"</span><span style="color: #000000;">.</span><span style="color: #000000;">"</span><span style="color: #000000;">))</span>&nbsp;<span style="color: #000000;">&gt;</span>&nbsp;<span style="color: #000000;">0</span><span style="color: #000000;">)</span><span style="color: #000000;">&nbsp;{<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property_name&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;substr</span><span style="color: #000000;">($</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;x</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;length</span><span style="color: #000000;">($</span><span style="color: #000000;">1</span><span style="color: #000000;">));</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;property_name&nbsp;</span><span style="color: #000000;">=</span>&nbsp;<span style="color: #000000;">$</span><span style="color: #000000;">1</span><span style="color: #000000;">;</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf</span><span style="color: #000000;">(</span><span style="color: #000000;">"</span><span style="color: #000000;">&lt;property&nbsp;name="%s"&nbsp;value="${%s}"/&gt;&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&nbsp;property_name</span><span style="color: #000000;">,</span>&nbsp;<span style="color: #000000;">$</span><span style="color: #000000;">1</span><span style="color: #000000;">);</span><span style="color: #000000;"><br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><img align="top" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" style="border: 0px; max-width: 100%;" />}</span></div><img src ="http://www.blogjava.net/stevenjohn/aggbug/421421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stevenjohn/" target="_blank">abin</a> 2014-12-15 20:15 <a href="http://www.blogjava.net/stevenjohn/archive/2014/12/15/421421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>