druid连接池使用系列问题

一、错误信息汇总

com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.
The last packet sent successfully to the server was 58129 seconds ago, which 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.

 

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 60009, active 50  
                at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)  
                at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:280)  
                ... 64 more  
Caused by: com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 60000, active 50  
                at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1071)  
                at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:898)  
                at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4544) 

 

 二、实战配置详解

   实战问题:druid连接池会保存一定的连接数,mysql也会按照自己的配置策略保存连接数,而且目前服务也是基于分布式,非常容易出现druid和mysql各自保持的连接不匹配

   方案目的:通过配置各自的连接持有策略,使druid和mysql之间的连接在性能允许的情况下,高度一致

   实施方案:

    1、druid配置

      连接数的基础配置、

        <!-- 配置初始化大小、最小、最大 -->
        <!-- 通常来说,只需要修改initialSize、minIdle、maxActive -->
        <property name="initialSize" value="10" />
        <property name="minIdle" value="10" />
        <property name="maxActive" value="500" />
        
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="10000" />

 

      无效连接的销毁配置:3种方式--testOnBorrow、testWhileIdle、removeAbandoned,优选testWhileIdle,详情参考知识点

        <property name="testWhileIdle" value="true" />  
        
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="60000" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="5000" />
        <!-- 校验SQL,不配置的话,testWhileIdle、testOnBorrow、testOnReturn无效 -->
        <property name="validationQuery" value="select 'x'" />

        <property name="removeAbandoned" value="true" />  
        <!-- 超时时间;单位为秒。180秒=3分钟 -->  
        <property name="removeAbandonedTimeout" value="180" /> 
        <!-- 关闭abanded连接时输出错误日志 -->  
        <property name="logAbandoned" value="true" />

 

    2、mysql配置

      主要配置两个属性:interactive_timeout、wait_timeout,两个参数的默认值都是28800s,8个小时

      interactive_timeout:交互式连接

      wait_timeout:非交互式连接

 三、知识点整理

  1、druid连接池

    druid连接池概念:http://blog.csdn.net/spring_great/article/details/48657027

    druid配置参数:http://blog.csdn.net/hj7jay/article/details/51686418

  2、druid清理无效连接

    三种方式清理:testOnBorrow、testWhileIdle、removeAbandoned

    testWhileIdle:timeBetweenEvictionRunsMillis 控制mysql心跳,意思是指定间隔时间检测一次连接的有效性,检测到连接失效,就释放连接,可以根据实际情况,调小间隔时间

     <!-- 自动检测,并关闭无效连接 -->
        <property name="testWhileIdle" value="true" />  
        
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="60000" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="5000" />
        <!-- 校验SQL,不配置的话,testWhileIdle、testOnBorrow、testOnReturn无效 -->
        <property name="validationQuery" value="select 'x'" />

    testOnBorrow:每次连接执行前,执行一条特定的SQL,校验连接的可用性,优点是保证连接的强可用,缺点是消耗性能,不建议使用

        <!-- 每次获取连接的时候,校验连接是否有效 -->
        <!-- <property name="testOnBorrow" value="true" />  -->     

    removeAbandoned:连接池的超时回收机制,获取一个连接后,最多使用的时间,单位是秒,管理的对象不是空闲连接

        <!-- 超过时间限制是否回收 -->  
        <!-- <property name="removeAbandoned" value="true" /> -->  
        <!-- 超时时间;单位为秒。180秒=3分钟 -->  
        <!-- <property name="removeAbandonedTimeout" value="180" /> -->  
        <!-- 关闭abanded连接时输出错误日志 -->  
        <!-- <property name="logAbandoned" value="true" /> -->

 

  3、查看数据库的连接情况

    命令:show processlist

    参数说明:http://blog.csdn.net/p656456564545/article/details/53169565

    操作截图:

    

 

   

 

 

问题 

  1、wait_timeout和interactive_timeout区别

    wait_timeout:非交互式连接,通过jdbc连接数据库是非交互式连接。

    interactive_timeout:交互式连接,说得直白一点,通过mysql客户端连接数据库是交互式连接

  2、wait_timeout和interactive_timeout 继承问题

    

posted @ 2017-11-15 21:47  刘广平  阅读(2093)  评论(0)    收藏  举报