4

如果我在空闲一段时间后启动我的应用程序,我会遇到以下错误。(我使用 Spring+Hibernate+MySQL 作为数据库)

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

我通过将以下内容添加到我的 servlet-context.xml 解决了这个问题。

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

我在这里问过这个问题,这个问题更具体到解决方案。我需要知道为什么会出现这个错误。

我尝试了上面链接中提供的第一个(使用 autoReconnect=true 配置连接字符串)和第三个选项(配置连接池以测试连接的有效性),并且两者都有效。我仍然不明白为什么我首先收到错误。

这是我更新的 servlet-context.xml 文件,我正在使用 ApacheDBCP 进行连接池。

<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:property name="validationQuery" value="SELECT 1"/>
</beans:bean>

是连接到期问题吗?请帮我理解。

4

1 回答 1

8

Here is the flow of events to illustrate what's happening:

  1. A connection is requested and used by the caller (application or connection pool)
  2. The caller keeps a reference to it so that the connection can be re-used
  3. The caller goes through a period of inactivity (for example, a dev system overnight or a QA system over the weekend).
  4. Once that database connection is not in use, the database considers the connection to be idle. Because it is idle, after a certain amount of time (MySQL default is 8 hours) the database closes the connection.
  5. The caller still has a handle to the connection, and when the caller tries to use the connection again unpleasantly discovers that connection has been closed.

The reason autoReconnect=true works, and that the pool testing the validity of the connection works, is that you are instructing the calling system to test the connection for this situation and to try again if this situation happens.

As for whether the validation query will affect performance: In theory it is using a connection to do something. In practice that something is so trivial that its effect is negligible in the context of your entire system.

[EDIT]

In this case Apache DBCP is the connection pool hanging on to the connection, but you do NOT want DBCP to close the connection after every call. The point of the connection pool is to keep a connection ready for the next call because creating connections is expensive. The connection objects maintained by the pool are backed by actual database connections, and the database is the one who closes that actual connection after the idle timeout period. Note that the timeout to close idle connections is configured on the database, not on the connection pool. Because of this, DBCP has no way of knowing whether the connection has been closed or not unless it actually tries to connect with it. That’s why you need a validation query.

For more information about configuring DBCP, see the configuration page and the API docs.

于 2013-06-12T12:34:58.317 回答