Seems to be an issue with my application. When ever the application is idle for a very long time ( am not sure the exact timing ) upon launching I get following error message in my logs. I am using Spring+Hibernate+MySQL and ApacheDBCP for connection pooling
ERROR [org.hibernate.util.JDBCExceptionReporter]The last packet successfully received from the server was 74,188,684 milliseconds ago.
The last packet sent successfully to the server was 74,188,685 milliseconds ago. 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.
org.hibernate.exception.JDBCConnectionException: could not execute query
If I relaunch the URL everything works fine. I assume that this is something to do with my connection pooling. Here is my Apache DBCP setting and wait_timeout in MYSQL is set to its default. (28800 Secs or 8Hrs ).
<beans:bean id="MyID" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mysql://localhost:17761/myDB"/>
<beans:property name="username" value="myname"/>
<beans:property name="password" value="mypwd"/>
<beans:property name="maxIdle" value="5"/>
<beans:property name="maxActive" value="20"/>
<beans:property name="minIdle" value="5"/>
</beans:bean>
While, I was searching I found this link The possible options for solving the issue.
Configure the connection string with autoReconnect=true
Will this help solving the issue ? In the same question mentioned above another author says, it is not advisable to go for this solution.
Increasing the value of MySQL server variable wait_timeout.
I am not sure how this will help in solving the error. if it helps, how to calculate the appropriate wait_timeout value. I mean what should be the criteria for this ?
Configuring the connection pool to test the validity of the connection.
After some study, I came to know that I can use validationQuery="SELECT 1" to check the connectivity before doing actual Query, will this help in resolving the issue ? Wont it affect the performance ?.
Issue is just not just specific to Apache DBCP, I have seen it in C3P0 also.
Please help.
Looks like I found the solution for it. All I did was to add below line in my servlet-context.xml. Still checking the performance impact of this line
<beans:property name="validationQuery" value="SELECT 1"/>
Question remains, Why was I actually getting the error ? :)
来源:https://stackoverflow.com/questions/16885323/connection-pool-issue-with-mysql-using-hibernate-and-apache-dbcp