When we began creating an application using Struts2 framework with Hibernate and MySQL, we ran into a problem which was related to MySQL timing out its connection after a span of 8 hours when left unused. It conceived a lot of time at our end but we were able to nail down the issue at the end.
Steps that we had taken to correct the issue related to Hibernate MySql connection timeout problem are listed below
– Download Hibernate C3P0 and copy .jar files
– Set c3p0.properties
– Make changes to hibernate.cfg.xml
– Test MySQL connection timeout
– Hibernate, MySQL connection timeout related error messages
Download Hibernate C3P0 (JDBC Data sources/Resource pools)
Copy the following .jar files into WEB-INF/lib folder
Add c3p0.properties file under the folder identifiable by java CLASSPATH (WEB-INF/classes) and add the below line
Open hibernate.cfg.xml and add the below set of lines
Restart tomcat instance.
To test MySQL connection timeout, open /etc/mysql/my.cnf (or my.ini file in windows) and under [mysqld] set (or modify) the wait_timeout value to say 120 seconds (2 minutes) to check how the application behaves after the above setting changes are made. You can reset it back once the testing is over. Default timeout setting in MySQL is 8 hours.
[mysqld] wait_timeout = 120
Some of the error messages associated with the hibernate mysql reconnect timeout issue are “org.hibernate.connection.C3P0ConnectionProvider cannot be found”, “org.hibernate.util.JDBCExceptionReporter: The last packet successfully received from the server was 73998 seconds ago”, “com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception”