繁体   English   中英

使用Hibernate和Apache DBCP的MySQL连接池问题

[英]Connection Pool Issue with MySQL using Hibernate and Apache DBCP

似乎与我的应用程序有关。 每当启动应用程序很长时间(不确定确切的时间)后,我的日志中都会出现以下错误消息。 我正在使用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中看到过。

请帮忙。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM