我有一个 grails 应用程序,它有一系列的高活动,但通常会持续几个小时到一整晚的不活动期。我注意到早上的第一批用户会遇到以下类型的异常,我相信这是由于池中的连接过时并且 MYSql 数据库关闭了它们。
我在谷歌搜索中发现了关于使用 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)