Saturday, June 25, 2011

Resolve Hibernate connection timeout issue with MySQL server

This problem has been faced by many of the developers but the fix is not very common. I had to do some research to find out the problem and solve it.
If you are using a MySQL server, the default connection timeout is 8 hours (tested on MySQL 5.2). This causes a problem in production environment as the database could not be connected overnight. Below is the log trace of the problem -

org.hibernate.util.JDBCExceptionReporter: The last packet successfully received from the server was 56697 seconds ago. The last packet sent successfully to the server was 56697 seconds ago, which  is longer than the server configured value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this problem.

As the description says, we can increase the wait_timeout property of MySQL server but it is not a good idea to keep stale connections open for long time. The 'autoReconnect=true' property did not work for me.

I figured out the below solution which worked perfectly for me.
First we need to modify our hibernate.cfg.xml file and add the following lines -


You have to add a new file in your classpath called The file contains -


The last but most important part is to add the required jars in the classpath. You must have hibernate.jar (contains org.hibernate.connection.C3P0ConnectionProvider class) and c3p0.jar.

This should solve the auto disconnection issue permanently.

P.S. To test the fix, you can change the wait_timeout property in my.cnf file (/etc/mysql/my.cnf in Linux) of MySQL server. Specify a small timeout value of few minutes.


  1. Hi

    Its throwing me the following error

    149819 [http-8080-5] ERROR org.hibernate.util.JDBCExceptionReporter - com.mchange.v2.c3p0.PoolBackedDataSource@42500667 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@8fda7516 [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge13a8ifwcgyn1g4zi4j|13d1402, idleConnectionTestPeriod -> 300, initialPoolSize -> 0, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 100, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 100, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 0, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@4e718292 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hge13a8ifwcgyn1g4zi4j|ed9f47, jdbcUrl -> jdbc:mysql://, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hge13a8ifwcgyn1g4zi4j|1b2d7df, numHelperThreads -> 3 ] has been closed() -- you can no longer use it.

    could you please explain me did you handler the hibernate session factory while writing your persistance code logic

  2. In case of a database operation consisting of multiple transactions, I open the session when I start the 1st transaction and keep it alive till the last transaction. After each transaction I flush the session but don't close it. Finally once the lase transaction is over, I close the session.

  3. Iam using mysql 5.5.Is it solve timeout issue in my project using your solution