2

似乎是我的应用程序的问题。当应用程序空闲很长时间(不确定确切的时间)时,我会在我的日志中收到以下错误消息。我正在使用 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>

在搜索的同时,我发现了此链接解决问题的可能选项。

  1. 使用 autoReconnect=true 配置连接字符串

    这有助于解决问题吗?在上面提到的同一问题中,另一位作者说,不建议采用这种解决方案。

  2. 增加 MySQL 服务器变量 wait_timeout 的值。

    我不确定这将如何帮助解决错误。如果有帮助,如何计算适当的 wait_timeout 值。我的意思是这应该是什么标准?

  3. 配置连接池以测试连接的有效性。

    经过一番研究,我知道我可以在进行实际查询之前使用 validationQuery="SELECT 1" 来检查连接性,这有助于解决问题吗?会不会影响性能?

问题不仅是 Apache DBCP 特有的,我也在 C3P0 中看到过。

请帮忙。

4

1 回答 1

1

看起来我找到了解决方案。我所做的只是在我的 servlet-context.xml 中添加以下行。仍在检查这条线的性能影响

<beans:property name="validationQuery" value="SELECT 1"/>

问题仍然存在,为什么我实际上得到了错误?:)

于 2013-06-06T04:12:19.533 回答