`
Jcat
  • 浏览: 49573 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Grails连MySQL - 连接池超时问题

阅读更多
问题表象
上线后的Grails应用,第二天,第一次登陆总是提示密码错误,再登陆一下就能成功。

推测倒是很准确
肯定是Tomcat或MySQL的某处超时设置导致的

准确诊断
因为无法在开发环境下模拟问题,先想办法在服务器端记录日志
Grails集成了log4j,用起来超级简单,而且还支持“环境”选项。

log4j = {
    appenders {
        environments {
            production {
                file name: 'file', file: '/home/jcat/myoalog/myoa.log'
            }
            development {
                file name: 'file', file: 'c:\\myoa.log'
            }
        }
    }

    root {
        info 'stdout', 'file'
    }

    info 'grails.app', 'myoa.StaticFilter'
}


然后在日志中发现了问题(这个log并不是我设计的,而是Shiro框架源码里的,可见人家是专业的)
2013-05-20 16:12:45,582 [http-bio-8080-exec-40] ERROR util.JDBCExceptionReporter  - The last packet successfully received from the server was 282,966,727 milliseconds ago.  The last packet sent successfully to the server was 282,966,728 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.

2013-05-20 16:12:45,601 [http-bio-8080-exec-40] INFO  app.realm  - Unable to authenticate with myoa.DbRealm - could not execute query; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute query

模拟错误
mysql> show global variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+
1 row in set (0.00 sec)


--重启后失效
set global wait_timeout=60;  --60秒

--需要停机配置
cat /etc/my.cnf
[mysqld]
wait_timeout=60

有趣的是,连接池超时后,登录后的浏览器会话并不会超时,所以第一次刷新会报错,再刷新(并不需要登录)就正常工作了。

最终解决
有3个途径
1. 调大MySQL的timout时间,网上说,最大到21天左右
2. 修改Hibernate的连接池组件,采用更高级的c3p0  http://blog.csdn.net/nethibernate/article/details/6658855
3. 修改默认的DBCP连接池的配置
http://www.netingcn.com/dbcp-config.html
http://sacharya.com/grails-dbcp-stale-connections

【法1-推荐】
        dataSource {   
          //解决数据库连接池长期不用超时的问题
            properties {
                timeBetweenEvictionRunsMillis = 1000 * 60 * 60 * 3  //每3小时检查一次
                minEvictableIdleTimeMillis = 1000 * 60 * 60 * 3     //把超过3小时的空闲连接激活一次
                //这样最差的情况,一个连接最多空闲6小时,而MySQL配置的是8小时断开
            }
        }


【法2-这会导致从连接池拿连接之前都会执行一次select 1语句】
                testOnBorrow = true
                testWhileIdle = false
                testOnReturn = false
                validationQuery = "SELECT 1"
分享到:
评论

相关推荐

    grails-fck-editor-0.3.zip_grails_grails-fck-editor

    标题中的"grails-fck-editor-0.3.zip_grails_grails-fck-editor"表明这是一个与Grails框架相关的插件,具体来说是FCKeditor的一个版本。FCKeditor是一款广泛使用的开源富文本编辑器,它允许用户在网页上创建和编辑...

    grails-datastore-gorm-plugin-support-2.0.4.RELEASE.zip

    总之,"grails-datastore-gorm-plugin-support-2.0.4.RELEASE.zip"提供了一个宝贵的资源,让开发者有机会学习和实践Grails的ORM功能和Android的MVC设计模式。无论是对Grails框架的探索,还是对Android开发的深化,这...

    grails-doc-CN-1.0.rar

    `grails-doc-CN-1.0.rar` 文件包含的是 Grails 1.0 版本的中文参考文档,对于那些不熟悉英文文档或者想要深入了解 Grails 的中文用户来说,这是一个极其宝贵的资源。 文档主要涵盖以下几个关键知识点: 1. **...

    groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part2

    groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part2 共两个包,解压后需要将扩展名.zip.bak改名为.zip重新解压。 http://dist.springsource.com/release/STS/3.8.1.RELEASE/dist/ e4.6/spring-tool-...

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    4. 版本兼容:虽然grails-fckeditor-0.9.5是较旧的版本,但理解其工作原理有助于我们理解如何集成第三方库,以及在更新到新版本时需要注意的问题。 总结,Grails框架及其中文文档为开发者提供了强大的工具和丰富的...

    groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1

    groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。

    grails-web-url-mappings-2.5.4.zip

    这个名为"grails-web-url-mappings-2.5.4.zip"的压缩包包含了Grails 2.5.4版本中的Web URL映射相关代码,让我们深入探讨这一关键组件。 Grails是一个基于Groovy语言的全栈式Java web框架,其设计理念是“简洁、生产...

    grails-docs-2.0.0

    综上所述,`grails-docs-2.0.0`不仅提供了Grails 2.0.0框架的全面介绍,还包含了详细的使用指南和API文档,是开发者学习、查阅和解决实际问题的关键资源。通过深入研究这些文档,开发者可以熟练掌握Grails框架,从而...

    grails-3.0.5-ga (2/2)

    以下是关于"grails-3.0.5-ga (2/2)"的详细知识点: 1. **Groovy语言**: Grails框架是用Groovy语言编写的,这是一种动态、面向对象的编程语言,与Java高度兼容,但语法更加简洁和灵活。 2. **MVC架构**: Grails遵循...

    Grails入门指南 -- 针对grails1.0.4更新

    ### Grails入门指南知识点 #### 一、Grails框架简介 - **背景**: Grails是一个基于Groovy语言的开源Web应用框架,适用于Java平台。它旨在简化开发过程,提高开发效率,尤其受到那些希望保留Java环境同时寻求更高效...

    Grails入门指南-中文版

    ### Grails入门指南-中文版 #### 一、引言 Grails是一个高效的Web开发框架,它基于Groovy编程语言并构建在Spring、Hibernate和其他Java框架之上。这使得Grails成为一个一站式的解决方案,极大地提高了开发者的生产...

    grails-docs-1.0

    《Grails框架API文档详解——基于grails-docs-1.0》 Grails是一种基于Groovy语言的开源Web应用框架,它简化了Java开发,提供了丰富的功能和强大的工具,深受开发者喜爱。本文将深入探讨grails-docs-1.0版本的API...

    grails-spring-websocket

    grails-spring-websocket ils子 2.4.x 3.2.7+ 2.5.x 4.0.0+ 安装 要将插件安装到Grails应用程序中,请将以下行添加到build.gradle依赖项部分: implementation "org.grails.plugins:grails-spring-websocket:...

    Grails-2.4.4-用户手册

    2. **创建新项目**:通过`grails create-app`命令创建项目,理解`grails-app`目录结构。 3. **编写Domain Class**:学习如何定义领域类,包括关系映射和验证规则。 4. **创建Controller**:了解如何创建控制器,处理...

    Grails开源框架 - 使用指南

    Grails开源框架 - 使用指南,版本1.0,中文文档。 Grails开源框架 - 使用指南 作者: Graeme Rocher, Marc Palmer 版本: 1.0 Copies of this document may be made for your own use and for distribution to ...

    grails-doc-1.0-beta1-中文.rar

    grails-doc-1.0-beta1-中文.rar。Grails 1.0 学习资料。

    grails-acegi-0.5.zip

    4. `docs`、`grails-app`、`src`、`scripts`和`lib`目录则分别包含了插件的文档、应用代码、源代码、脚本以及依赖的库文件。 Grails Acegi 0.5插件提供了以下主要功能: - **用户认证**:支持多种认证机制,如...

    grails-docs-1.0.3

    grails参考文档 The Grails Framework - Reference Documentation Authors: Graeme Rocher, Marc Palmer Version: 1.0.3

    grails-datastore-gorm-tck-1.0.9.RELEASE.zip

    标题 "grails-datastore-gorm-tck-1.0.9.RELEASE.zip" 提供的信息表明,这是一个与Grails框架相关的数据存储(Datastore)和GORM(Grails Object Relational Mapping)测试兼容性工具包(Test Compatibility Kit,...

Global site tag (gtag.js) - Google Analytics