我的申请似乎有问题。每当应用程序在启动时闲置很长时间(我不确定确切的时间)时,我都会在日志中收到以下错误消息。我正在使用 Spring+Hibernate+MySQL 和 ApacheDBCP 进行连接池
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
如果我重新启动 URL,一切正常。我认为这与我的连接池有关。这是我的 Apache DBCP 设置,MYSQL 中的 wait_timeout 设置为默认值。 (28800 秒或 8 小时)。
<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>
同时,我在搜索时发现了这个 link解决问题的可能选项。
使用autoReconnect=true配置连接字符串
这是否有助于解决问题?在上面提到的同一个问题中,另一位作者说,不建议采用这种解决方案。
增加 MySQL 服务器变量 wait_timeout 的值。
我不确定这将如何帮助解决错误。如果有帮助,如何计算适当的 wait_timeout 值。我的意思是这应该是什么标准?
配置连接池,测试连接有效性。
经过一些研究,我知道我可以在执行实际查询之前使用 validationQuery="SELECT 1"检查连接,这是否有助于解决问题?它不会影响性能吗?
问题不仅仅特定于 Apache DBCP,我也在 C3P0 中看到过。
请帮忙。
最佳答案
看来我找到了解决方案。 我所做的只是在我的 servlet-context.xml 中添加以下行。仍在检查这条线的性能影响
<beans:property name="validationQuery" value="SELECT 1"/>
问题仍然存在,为什么我实际上得到了错误? :)
关于java - 使用 Hibernate 和 Apache DBCP 的 MySQL 的连接池问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16885323/