• 解决mysql连接异常—com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception


    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>

     

  • 相关阅读:
    字典序算法
    C语言中strcpy,strcmp,strlen,strcat函数原型
    堆和栈概念整理
    distribution counting—a sorting method from aocp
    矩阵访问测试
    一个很大的数组,如何高效的把零都移到前面
    poj1083
    AXD+HJTAG环境搭建总结
    软件断点和硬件断点的区别和数量限制
    Ubuntu下用as汇编器编写hello.S文件
  • 原文地址:https://www.cnblogs.com/raym/p/5130587.html
Copyright © 2020-2023  润新知