Hibernate and MySQL Connection Timeouts
Several weeks ago, while trying to finish up a portion of my senior project, my team was having some issues with our persistence layer. The issue had to do with the connection time out in MySQL, and the default C3P0 setup that Hibernate uses. As it took MUCH more research than I think it should have to solve the issue, I thought I would write a blog entry about it. Besides, I haven't set out to give anything back in quite a while. Note that while none of this information is new in any way, I have not been able to find it all gathered together in one place.
Complaint:
After my Spring/Hibernate/MySQL soltuion has been running but inactive for more than 8 hours, I get a broken pipe exception.
Root Cause:
MySQL automatically times out, and closes unused connections after 8 hours, and out of the box, Hibernate does not set up C3P0 to appropriately test/refresh its connection pool when connections go stale.
Solution:
It seemed that as we searched the forums for an answer it wasn't immediately obvious what the right answer would be. We noticed that the C3P0 properties had different names, depending on how your project was configured. The hibernate documentation has some suggestions, but it turns out that the names of the properties are not stated correctly. It is also important to note that if properties are not set in the hibernate configuration they will be overridden by hibernate defaults. Here's a snippet from my hibernate.cfg.xml:
<session-configuration>
<!-- driver/connection info removed --!>
<!-- C3P0 Stuff -->
<property name=“hibernate.c3p0.acquire_increment”>3</property>
<property name=“hibernate.c3p0.idle_test_period”>14400</property>
<property name=“hibernate.c3p0.timeout”>25200</property>
<property name=“hibernate.c3p0.max_size”>15</property>
<property name=“hibernate.c3p0.min_size”>3</property>
<property name=“hibernate.c3p0.max_statements”>0</property>
<property name=“hibernate.c3p0.preferredTestQuery”>select 1;</property>
</session-configuration>
The important properties to note above are idle_test_period, and timeout. You want to make sure that C3P0 is configured to test for closed connections and time out unused connections at some rate beneath the threshold set on your MySQL server. With these properties in place you should be good to go.
Testing:
Waiting 8 hours to conduct a test like this would be lame, so let's just change the connection timeout for the MySQL server, re-start the MySQL server, and our application to try it out. You can change the timeout time for MySQL by editing your /ect/my.cnf file (linux) or your my.ini file(windows). You would want to add the following line to the file:
wait_timeout=120
Note that the value after the property is in seconds. Once you're done with your testing, you can remove the property and it will default back to 8 hours.
Set the MySQL Connection Timeout
Labels: c3p0, connection-timout, hibernate, mysql, spring

13 Comments:
Hi:
I noticed that you have "hibernate.c3p0.idle_test". But from my research with c3p0 and hibernate, they said that variable should be "hibernate.c3p0.idle_test_period". You alluded to the fact that the hibernate document incorrected stated the name of the properties, which property names were incorrect? Was it the idle_test?
thanks,
Mona
Mona-
You are totally correct, I wrote this entry several weeks after I had fixed the problem, and in my writeup, I did mis-state the property for the idle test. It is as you say it should be and I have fixed the writeup to reflect the proper property name.
Thank You for noticing,
Joe
Hi Joe:
Thanks for your fast response!
We are still having the problem with MySQL timeout. We are using MySQL 5.0.41, hibernate 2, c3p0 0.9.2. My MySQL timeout is set to 900 and my hibernate.properties has:
hibernate.show_sql=false
hibernate.connection.pool_size=40
hibernate.connection.autocommit=true
hibernate.connection.shutdown=true
hibernate.cache.provider_class=net.sf.hibernate.cache.EhCacheProvider
###########################
### C3P0 Connection Pool###
###########################
hibernate.c3p0.max_size 50
hibernate.c3p0.min_size 2
hibernate.c3p0.timeout 600
hibernate.c3p0.max_statements 0
hibernate.c3p0.idle_test_period 100
hibernate.c3p0.idle_test 450
hibernate.c3p0.acquire_increment 2
Do you have any suggestion for us?
Unfortunately, the code is not ours. I do have the source but I don't even know what to look for in the code to see if it is doing the right thing with using the DB/hibernate. Can you suggest something I should check in the code?
thanks,
Mona
Is it just a transcription error in the properties that you pasted in your last response, or are the equals (=) signs missing from all of your C3P0 Connection Pool properties?
Shouldn't it look like this:
hibernate.c3p0.max_size=50
Give that a try.
I think that you shouldn't prefix the property names with the "hibernate" keyword when you are using an xml configuration file.
So <property name="hibernate.c3p0.timeout"> should read <property name="c3p0.timeout">
isn't the property in question wait_timeout, not connect_timeout? looks like wait_timeout is the 8-hour default mysql property? real curious about this.
Matt,
You're right!
I was writing this up a month or so after I fixed the issue, and it appears that when I went to write it up I used the wrong system variable. My apologies. I'll fix the post.
Thanks very much! This saved me a lot of time fixing this problem on our system.
Very good tip, thank you
Hi,
I have a probleme, i lost my connection.
I use hibernate2.jar, c3p0-0.9.1.2.jar, mysql5, java 1.5
My hibernate.cfg.xml :
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://mysql-2.univ-rennes1.fr:3307/igUR1prod10</property>
<property name="connection.username">dev_igUR1prod10</property>
<property name="connection.password">6sQxbPl9c</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.autoReconnect">true</property>
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">10</property>
<property name="c3p0.acquireRetryAttempts">30</property>
<property name="c3p0.acquireIncrement">5</property>
<property name="c3p0.idleConnectionTestPeriod">9</property>
<property name="c3p0.initialPoolSize">20</property>
<property name="c3p0.maxPoolSize">100</property>
<property name="c3p0.maxIdleTime">300</property>
<property name="c3p0.maxStatements">50</property>
<property name="c3p0.minPoolSize">10</property>
Do you have any suggestion for us?
Thanks Linda
This didn't work for me,
but this context connection pool solved my problem:
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/dbname"
maxActive="30"
maxIdle="5"
validationQuery="SELECT 1"
username="username"
password="password"
/>
Great post! Been struggling with this one for I don't know how long.
Yes! Thank you.
If anyone wants to know how to fix this in a spring context, the property names look like this:
bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
property name="idleConnectionTestPeriod" value="14400"
property name="maxIdleTime" value="25200"
Post a Comment
<< Home