DBCP连接池连接MySql数据库时,一奇葩数据库设置为30秒内无请求自动断开。超时后链接无法关闭,活动链接数飞奔,最后挂掉。

网上找了一圈,一般是这三种,方法一pass,方法二测试无效可能设置错了吧,再者本身30秒已经够短了,最后落在了方法三,

BasicDataSource ds = new BasicDataSource(); 
        ds.setTimeBetweenEvictionRunsMillis(1000*20);
        ds.setTestWhileIdle(true);
        ds.setValidationQuery("SELECT 1 ");

最终解决。

感谢http://www.tuicool.com/articles/iM3qqyu

解决方案( 解决这个问题的办法有三种,推荐第二种 ):

1. 增加 MySQL 的 wait_timeout 属性的值

修改mysql安装目录下的配置文件 my.ini文件(如果没有此文件, 复制“my-default.ini”文件,生成“复件 my-default.ini”文件。 将“复件 my-default.ini”文件重命名成“my.ini” ),在文件中设置:

wait_timeout=31536000
interactive_timeout=31536000

这两个参数的默认值是8小时(60*60*8=28800)。

2. 减少连接池内连接的生存周期

减少连接池内连接的生存周期, 使之小于 上一项中所设置的 wait_timeout 的值

修改 c3p0 的配置文件, 在 Spring 的配置文件中设置:

  <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="1800"/>
<!--other properties -->
</bean>

3. 定期使用连接池内的连接

定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。

修改 c3p0 的配置文件,在  Spring 的配置文件中设置:

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="preferredTestQuery" value="SELECT 1"/>
<property name="idleConnectionTestPeriod" value="18000"/>
<property name="testConnectionOnCheckout" value="true"/>
<!--other properties --></bean>

解决mysql连接异常—-com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception的更多相关文章

  1. SSH项目过一段时间之后再访问会报一次Could not open Hibernate session for transaction 异常,Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlyi,再重新方法即可访问成功(通常出现在过了一晚之后再去访问系统)

    前端时间到客户那去进行项目的上线测试,将项目部署好之后,运行都是正常的,可是每到了第二天早上访问的时候,就会报一个Could not open Hibernate session for transa ...

  2. 解决MySQL连接超时Communications link failure due to underlying exception

    最近在用一个MySQL的Java连接池的过程中,连接一晚上不释放,第二天就会造成超时的错误,查了一下原因,原来是因为MySQL默认的空闲等待时间是8个小时,一旦空闲超过8个小时,就会抛出异常.异常文本 ...

  3. 异常解决:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    异常描述 这个异常通常有如下信息: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failu ...

  4. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法

    09:00:30.307 [http-8080-6] ERROR org.hibernate.transaction.JDBCTransaction -JDBC begin failed com.my ...

  5. mysql 5.1超过默认8小时空闲时间解决办法(错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure)

    报错: MySQL第二天早上第一次连接超时报错, com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications lin ...

  6. mysql连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    现象:客户端连接服务器端mysql是没问题的,所有都木有问题,应用程序配置也没问题,但是连接就抛异常: com.mysql.jdbc.exceptions.jdbc4.CommunicationsEx ...

  7. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决

    感谢大佬:https://blog.csdn.net/a704397849/article/details/93797529 springboot + mybatis多数据库 + druid连接池配置 ...

  8. Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    很长的报错,截取   ERROR c.a.d.p.DruidDataSource - discard connection   com.mysql.jdbc.exceptions.jdbc4.Comm ...

  9. 解决Java程序连接mysql数据库出现CommunicationsException: Communications link failure错误的问题

    一.背景 最近在家里捣鼓一个公司自己搭建的demo的时候,发现程序一启动就会出现CommunicationsException: Communications link failure错误,经过一番排 ...

随机推荐

  1. 函数的引用透明性(referential transparency)

    1. 基础 初学程序设计时,比较容易混淆的两个概念是数学函数(math function)和程序中使用的函数. 在数学函数中 y=f(x),一个输入值有固定的输出值.例如,无论计算多少次,sinπ 的 ...

  2. angular4开发过程中遇到的问题和知识点记录

    1. angular2中的属性有什么区别,为什么会报错呢? 元素上有两种属性:property和attribute,attribute是通过getAttribute()和setAttribute()方 ...

  3. Spring Boot 静态资源处理(转)

    Spring Boot 静态资源处理 Spring Boot 系列 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Bo ...

  4. NSString常见用法

    1.创建常量字符串 NSString *str = @"Hello World!"; 2.创建空字符串,给予赋值 NSString *str = [[NSString alloc] ...

  5. 22、在Ubuntu 14.0上使用韦东山IP2977测试总结(未成功)

    1. 去www.kernel.org下载同版本的内核(与Ubuntu 14.0) 解压后把drivers/media/video/uvc目录取出(发现我的3.13版本的在drivers\media\u ...

  6. VS_VERSION_INFO

    VS_VERSION_INFO这里可以修改ocx的版本号

  7. sublim课程2 sublim编辑器的使用(敲代码的时候把这个放旁边用)

    sublim课程2   sublim编辑器的使用(敲代码的时候把这个放旁边用) 一.总结 一句话总结:不必一次记住所有,不可能也得不偿失,先记住常用,慢慢来.(敲代码的时候把这个放旁边用,一下子就熟了 ...

  8. Docker入门之 - 更换源为国内源,实现快速下载image

    原文:Docker入门之 - 更换源为国内源,实现快速下载image 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012055638/artic ...

  9. Chrome源代码结构

    首先,开始接触Chrome的童鞋可能有一个疑惑,Chrome和Chromium是同一个东西吗?答案是,Chrome是Google官方的浏览器项目名称,Chromium是Google官方对Chrome开 ...

  10. win7注册表常用设置

    win7注册表常用设置 一.总结 一句话总结:regedit可以修改很多东西,电脑时间,背景,u盘读写,鼠标右键情况. 二.win7注册表常用设置 一. 秀出自我风格的屏幕保护画面 1.气泡屏幕保护 ...