【问题标题】:Connection Pool Issue with MySQL using Hibernate and Apache DBCP使用 Hibernate 和 Apache DBCP 的 MySQL 连接池问题
【发布时间】:2013-05-28 22:19:15
【问题描述】:

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

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

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

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

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

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

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

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

请帮忙。

【问题讨论】:

    标签: java mysql hibernate connection-pooling apache-commons-dbcp


    【解决方案1】:

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

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

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

    【讨论】:

    • 也许您可以链接到有关您为什么会收到错误的其他问题。至于验证查询是否会影响性能:理论上是使用连接做某事。在实践中,某些事情是如此微不足道且运行如此罕见,以至于在整个系统的上下文中其影响可以忽略不计。
    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2012-07-06
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2011-02-08
    • 2019-12-02
    相关资源
    最近更新 更多