<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>off-topic &#187; MySQL</title>
	<atom:link href="http://off-topic.biz/en/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://off-topic.biz/en</link>
	<description>A collection of off-topic posts</description>
	<lastBuildDate>Thu, 07 Apr 2011 09:16:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Tomcat + Hibernate + C3P0 + MySQL: Communications link failure</title>
		<link>http://off-topic.biz/en/tomcat-hibernate-c3p0-mysql-communications-link-failure/</link>
		<comments>http://off-topic.biz/en/tomcat-hibernate-c3p0-mysql-communications-link-failure/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 15:27:14 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[connection pooling]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://off-topic.biz/en/?p=16</guid>
		<description><![CDATA[After deploying a Google GWT application using Hibernate and C3P0 connection pooling backed by a MySQL database server to a test server running Tomcat everything seemed to run correctly. But letting the server run over night a login attempt failed &#8230; <a href="http://off-topic.biz/en/tomcat-hibernate-c3p0-mysql-communications-link-failure/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After deploying a <a href="http://code.google.com/webtoolkit/">Google GWT</a> application using <a href="http://www.hibernate.org/">Hibernate</a> and C3P0 connection pooling backed by a <a href="http://mysql.com/">MySQL</a> database server to a test server running <a href="http://tomcat.apache.org/">Tomcat</a> everything seemed to run correctly. But letting the server run over night a login attempt failed the next day. A quick search showed me that I am not the only one on this planet who ran into the following error:</p>
<pre>org.hibernate.exception.JDBCConnectionException:
  could not execute query
[...]
com.mysql.jdbc.CommunicationsException:
  Communications link failure due to underlying exception:
[...]</pre>
<p>This problem occurs because MySQL drops idle JDBC connection after 8 hours, but the C3P0 connection pooling mechanism still considers these connection as alive and valid.</p>
<p>I found several possible solution on the Internet. Some people recommend to set the &#8220;autoReconnect&#8221; parameter in the JDBC URL, but this is <a href="http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html">not recommended</a> by the MySQL team.</p>
<p>Others recommend to set the C3P0 properties accordingly in order to check and close idle database connections. In most cases the proposed solution looks like the <a href="http://www.hibernate.org/214.html">&#8220;official&#8221;</a> configuration provided by the Hibernate crew:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;c3p0.min_size&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;c3p0.max_size&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>25<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;c3p0.acquire_increment&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;c3p0.max_statements&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>50<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;c3p0.timeout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>14400<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;c3p0.idle_test_period&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>3600<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>But as I have found in many, many comments in discussion groups this also did not work in a large number of cases. In order to get C3P0 connection pooling in Hibernate3 work correctly with a MySQL database one important property setting is still missing. I found this hint in a comment to the above &#8220;official&#8221; configuration. In addition one has to explicitly set the connection provider class:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;connection.provider_class&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  org.hibernate.connection.C3P0ConnectionProvider
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>After setting this property the above C3P0 configuration works correctly. At least for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://off-topic.biz/en/tomcat-hibernate-c3p0-mysql-communications-link-failure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

