alibaba / druid

阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
https://github.com/alibaba/druid/wiki
Apache License 2.0
28k stars 8.59k forks source link

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

Closed slieer closed 1 year ago

slieer commented 6 years ago

和这个有相似之外: https://github.com/alibaba/druid/issues/2423

我昨天也遇到了,采用的是如下的配置,在大量请求持续运行时,曾重启过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" />  
slieer commented 6 years ago

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 奔溃!

slieer commented 6 years ago

init方法 , 就不应该搞无锁的编程方式。 我设置的连接数量参数,为什么会无效?

slieer commented 6 years ago

在初始化连接池时, 一定要校验一下当前该数据库共有多少个真实的连接。 最好提供控制某个数据库的最大连接数据的参数(一个业务模块一个连接池,而当多个业务对应一个数据库实例时, 更需要DB层面的最大连接数校验)。

wenshao commented 6 years ago

init出错需要,还是需要调用close方法将DruidDataSource关闭。

slieer commented 6 years ago

感谢你的回复。 但是能不能在DruidDataSource, 做好校验呢? 比如发现数据库接连数, 达到一定的数量后,无论调用多少次init, 都不予分配置物理连接。 岂不更好? DruidDataSource的可靠性为什么要依赖于上层模块呢?

Force-King commented 5 years ago

你好,楼主,druid 目前用哪个版本最好?