MySQL连接超时问题 - 使用Hibernate和ORM在Tomcat上的Grails应用程序

gav*_*gav 21 mysql grails tomcat timeout

我在VPS上的Ubuntu上有一个运行在Tomcat上的小型grails应用程序.我使用MySql作为我的数据存储区,一切正常,除非我离开应用程序超过半天(8小时?).我做了一些搜索,显然这是wait_timeoutmysql.cnf中的默认值,所以8小时后连接会死,但Tomcat不会知道,当下一个用户试图查看该站点时,他们会看到连接失败错误.刷新页面将解决此问题,但我想完全摆脱错误.对于我的MySql版本(5.0.75),我只有my.cnf而且它不包含这样的参数.在任何情况下,更改此参数都无法解决问题.

这篇Blog Post似乎报告了一个类似的错误,但我仍然不完全理解我需要配置什么来解决这个问题,而且我希望有一个比另一个第三方库更简单的解决方案.我正在运行的机器有256MB内存,我正在努力使程序/服务的运行数量降到最低.

我可以在Grails/Tomcat/MySql中配置什么来让它消失吗?

提前致谢,

GAV

从我的Catalina.out;

2010-04-29 21:26:25,946 [http-8080-2] ERROR util.JDBCExceptionReporter  - The last packet successfully received from the server was 102,906,722 milliseconds$
2010-04-29 21:26:25,994 [http-8080-2] ERROR errors.GrailsExceptionResolver  - Broken pipe
java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
         ...
2010-04-29 21:26:26,016 [http-8080-2] ERROR util.JDBCExceptionReporter  - Already closed.
2010-04-29 21:26:26,016 [http-8080-2] ERROR util.JDBCExceptionReporter  - Already closed.
2010-04-29 21:26:26,017 [http-8080-2] ERROR servlet.GrailsDispatcherServlet  - HandlerInterceptor.afterCompletion threw exception
org.hibernate.exception.GenericJDBCException: Cannot release connection
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Already closed.
        at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:84)
        at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:181)
        ... 1 more
Run Code Online (Sandbox Code Playgroud)

fab*_*474 18

参考本文,您的DBCP连接池中存在过时的连接,这些连接由操作系统或防火墙静默删除.

解决方案是定义验证查询,并在您在应用程序中实际使用它之前对连接进行完整性检查.在grails中,这实际上是通过修改grails-app/conf/spring/Resource.groovy文件并添加以下内容来完成的:

beans = {
  dataSource(BasicDataSource) {
    //run the evictor every 30 minutes and evict any connections older than 30 minutes.
    minEvictableIdleTimeMillis=1800000
    timeBetweenEvictionRunsMillis=1800000
    numTestsPerEvictionRun=3
    //test the connection while its idle, before borrow and return it
    testOnBorrow=true
    testWhileIdle=true
    testOnReturn=true
    validationQuery="SELECT 1"
  }
} 
Run Code Online (Sandbox Code Playgroud)


Tat*_*ela 5

在grails 1.3.X中,您可以修改DataSource.groovy文件中的退出者值,以确保空闲期间使用池化连接。这将确保mysql服务器不会使连接超时。

production {
  dataSource {
    pooled = true
    // Other database parameters..
    properties {
       maxActive = 50
       maxIdle = 25
       minIdle = 5
       initialSize = 5
       minEvictableIdleTimeMillis = 1800000
       timeBetweenEvictionRunsMillis = 1800000
       maxWait = 10000
    }
}
Run Code Online (Sandbox Code Playgroud)

验证此工作方式的一种快速方法是修改MySQL my.cnf配置文件[mysql]元素,并添加一个具有较低值的wait_time参数。