开发者社区> 问答> 正文

druid 1.0.20和最新版的1.1.10会出现严重的连接泄露情况

采用的是如下的配置,在大量请求持续运行时,曾重启过myql, tomcat, 添加过/etc/hosts文件数据库本地域名(本地域名,可指向本地,远程db), 瞬间在mysql 端出现过近3000 client会话, 但多数是sleep状态的! 另外,要说明一下本地域名指向的数据库ip改了,也应该连新ip上的db啊. 根据下面的配置, 怎么也想不通啊。 今天在压测环境试了重显不了。

druid 1.0.20




<property name="initialSize" value="1" />
<!-- 连接池最大使用连接数量 -->  
<property name="maxActive" value="20" />  
<!-- 连接池最小空闲 -->
<property name="minIdle" value="1"/> 
<!-- 获取连接最大等待时间 --> 
<property name="maxWait" value="60000" />  
<property name="validationQuery" value="${validationQuery}" />  
<property name="testOnBorrow" value="false" />  
<property name="testOnReturn" value="false" />  
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
<property name="minEvictableIdleTimeMillis" value="25200000" /> 
<!-- 打开removeAbandoned功能 --> 
<property name="removeAbandoned" value="true" /> 
<!-- 单位是秒 --> 
<property name="removeAbandonedTimeout" value="1800" />  
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />  

22:16:25.619 INFO com.alibaba.druid.pool.DruidDataSource 722 init - {dataSource-1} inited 22:16:25.621 WARN org.springframework.beans.factory.support.AbstractBeanFactory 1490 getTypeForFactoryBean - Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vipRenewSmsRecordMapper' defined in URL [jar:file:/opt/tomcat8/webapps.01/user/WEB-INF/lib/userpoint-model-1.0.0.RELEASE.jar!/com/xxxxx`digital/userpoint/dao/VipRenewSmsRecordMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dynamicDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamicDataSource' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dataSourceUser' while setting bean property 'targetDataSources' with key [TypedStringValue: value [userDB], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUser' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: java.util.Collections$SynchronizedMap cannot be cast to java.util.IdentityHashMap; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dynamicDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamicDataSource' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dataSourceUser' while setting bean property 'targetDataSources' with key [TypedStringValue: value [userDB], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUser' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: java.util.Collections$SynchronizedMap cannot be cast to java.util.IdentityHashMap 22:16:25.661 INFO com.alibaba.druid.pool.DruidDataSource 722 init - {dataSource-2} inited 22:16:25.662 WARN org.springframework.beans.factory.support.AbstractBeanFactory 1490 getTypeForFactoryBean - Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'VIPPointMapper' defined in URL [jar:file:/opt/tomcat8/webapps.01/user/WEB-INF/lib/userpoint-model-1.0.0.RELEASE.jar!/com/xxxxxdigital/userpoint/dao/VIPPointMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dynamicDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamicDataSource' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dataSourceUser' while setting bean property 'targetDataSources' with key [TypedStringValue: value [userDB], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUser' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: java.util.Collections$SynchronizedMap cannot be cast to java.util.IdentityHashMap; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dynamicDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamicDataSource' defined in class path resource [spring-mybatis.xml]: Cannot resolve reference to bean 'dataSourceUser' while setting bean property 'targetDataSources' with key [TypedStringValue: value [userDB], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUser' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: java.util.Collections$SynchronizedMap cannot be cast to java.util.IdentityHashMap

疯狂的刷上述日志, mysql 端的连接疯狂的增长, 直到mysql too many , jvm 奔溃!init方法 , 就不应该搞无锁的编程方式。 我设置的连接数量参数,为什么会无效?

原提问者GitHub用户slieer

展开
收起
山海行 2023-07-05 19:52:21 690 0
2 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    面是一些可能导致连接泄露的原因:

    连接未正确关闭。如果应用程序未正确关闭连接,例如在异常情况下未关闭连接,或者未正确释放连接,连接池就会出现连接泄露。

    连接泄漏检测机制未开启。在Druid连接池中,可以开启连接泄漏检测机制,以检测并关闭未正确关闭的连接,防止连接泄露。如果未开启该机制,连接池就无法检测并关闭未正确关闭的连接,导致连接泄露。

    连接泄漏检测机制配置不正确。在Druid连接池中,可以配置连接泄漏检测机制的参数,例如 timeBetweenEvictionRunsMillis、minEvictableIdleTimeMillis 等,以控制检测频率和超时时间。如果这些参数配置不正确,就可能导致连接泄漏。

    为了避免连接泄露问题,您可以考虑执行以下操作:

    考虑升级到最新版本的Druid连接池,最新版本的Druid连接池修复了一些连接泄露问题。

    启用Druid连接池的连接泄漏检测机制,并配置相应的参数。例如,可以使用 timeBetweenEvictionRunsMillis 参数控制检测频率,使用 minEvictableIdleTimeMillis 参数控制连接空闲的最小时间,以便于检测并关闭未正确关闭的连接。

    确保在应用程序中正确地关闭连接,特别是在异常情况下,应该使用 try-catch-finally 语句块确保连接正确关闭,或者使用 try-with-resources 语句,以确保连接在使用完毕后自动关闭。

    可以在应用程序中使用连接池监控工具,例如Druid提供的监控页面,或者使用其他第三方监控工具,以帮助您及时发现和处理连接泄露问题。

    2023-07-30 15:07:57
    赞同 展开评论 打赏
  • init出错需要,还是需要调用close方法将DruidDataSource关闭。

    原回答者GitHub用户wenshao

    2023-07-06 11:09:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载