我有一个Grails应用程序,活动会突然变得非常高,但是通常会有数小时到整夜的不活动期。我注意到早上的第一批用户会遇到以下类型的异常,我认为这是由于连接池中的连接变得过时并且MySQL数据库关闭了它们导致的。
在Google搜索中,我发现了关于是否使用Connector/J连接属性“autoReconnect=true”的矛盾信息(以及即使连接被恢复,客户端是否仍会收到异常),或者设置其他属性以定期驱逐或刷新空闲连接,借用测试等。 Grails在DBCP下面使用。 我目前拥有如下简单的配置,并正在寻找如何最好地确保从长时间不活动期后抓取的任何连接都是有效且未关闭的答案。
在Google搜索中,我发现了关于是否使用Connector/J连接属性“autoReconnect=true”的矛盾信息(以及即使连接被恢复,客户端是否仍会收到异常),或者设置其他属性以定期驱逐或刷新空闲连接,借用测试等。 Grails在DBCP下面使用。 我目前拥有如下简单的配置,并正在寻找如何最好地确保从长时间不活动期后抓取的任何连接都是有效且未关闭的答案。
dataSource {
pooled = true
dbCreate = "update"
url = "jdbc:mysql://my.ip.address:3306/databasename"
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
username = "****"
password = "****"
properties {
//what should I add here?
}
}
异常
2012-06-20 08:40:55,150 [http-bio-8443-exec-1] ERROR transaction.JDBCTransaction - JDBC begin failed
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 64,129,968 milliseconds ago. The last packet sent successfully to the server was 64,129,968 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.
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3851)
...... Lots more .......
Caused by: java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)