2013-06-02 117 views
2

似乎是我的应用程序的问题。当应用程序长时间闲置(不确定确切的时间)后,我会在我的日志中看到以下错误消息。我正在使用Spring + Hibernate + MySQL和ApacheDBCP进行连接池MySQL使用休眠和Apache DBCP连接池问题

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的=真

    这能否帮助解决这个问题?在上面提到的同一个问题中,另一位作者说,这个解决方案并不可取。

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

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

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

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

问题不只是特定于Apache DBCP,我也看到它在C3P0中。

请帮忙。

回答

1

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

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

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

+0

也许你可以链接到你的其他问题,为什么你会得到错误。至于验证查询是否会影响性能:理论上它使用连接来执行某些操作。在实践中,某些东西如此微不足道,并且很少运行以至于它在整个系统中的影响可以忽略不计。 – Jay