sql 连接数不释放 ,Druid异常:wait millis 40000, active 600, maxActive 600

 

Hibernate + Spring + Druid 数据库mysql

 

由于配置如下

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<bean id= "dataSource"  class = "com.alibaba.druid.pool.DruidDataSource"  destroy-method= "close" >
         <property name= "url"  value= "${datasource.url}"  />
         <property name= "username"  value= "${datasource.username}"  />
         <property name= "password"  value= "${datasource.password}"  />
         <!-- 驱动类 -->
         <property name= "driverClassName"  value= "com.mysql.jdbc.Driver"  />
 
         <!-- 默认自动提交状态。如果不设置,则setAutoCommit 方法将不被调用 -->
         <property name= "defaultAutoCommit"  value= "true"  />
         <!-- 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall -->
         <!-- <property name= "filters"  value= "stat"  /> -->
         <property name= "filters"  value= "config,stat,log4j"  />
         <!-- 可以在这个池中同时被分配的有效连接数的最大值 -->
         <property name= "maxActive"  value= "600"  />
         <!-- 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 -->
         <property name= "initialSize"  value= "2"  />
         <!-- 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为 true 使用非公平锁。-->
         <property name= "maxWait"  value= "40000"  />
         <!-- 最小连接池数量 -->
         <property name= "minIdle"  value= "0"  />
         <!-- 有两个含义: 1 ) Destroy线程会检测连接的间隔时间   2 ) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 -->
         <property name= "timeBetweenEvictionRunsMillis"  value= "60000"  />
         <!-- Destory线程中如果检测到当前连接的最后活跃时间和当前时间的差值大于 minEvictableIdleTimeMillis,则关闭当前连接 -->
         <property name= "minEvictableIdleTimeMillis"  value= "8000"  />
         <!-- 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为 null ,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。
              在mysql中通常为select  'x' ,在oracle中通常为select  1  from dual -->
         <property name= "validationQuery"  value= "select 1 from dual"  />
         <!-- 建议配置为 true ,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->
         <property name= "testWhileIdle"  value= "false"  />
         <!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。-->
         <property name= "testOnBorrow"  value= "false"  />
         <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
         <property name= "testOnReturn"  value= "false"  />
         <!-- 要启用PSCache,必须配置大于 0 ,当大于 0 时,poolPreparedStatements自动触发修改为 true 。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说 100  -->
         <property name= "maxOpenPreparedStatements"  value= "200"  />
         <!-- 对于建立时间超过removeAbandonedTimeout的连接强制关闭 -->
         <property name= "removeAbandoned"  value= "true"  /> <!-- 打开removeAbandoned功能 -->
         <!--  180 秒,也就是 3 分钟 -->
         <property name= "removeAbandonedTimeout"  value= "1800"  />
         <!-- 关闭abanded连接时输出错误日志 -->
         <property name= "logAbandoned"  value= "true"  />
     </bean>

 

从配置可以看到,我的连接数max = 600, 但是程序跑到不到一会就报连接数不够, 获取不了连接数就一直卡在那里,重启tomcat服务器,过一会又是这样

 

从数据库  SHOW PROCESSLIST 发现好多连接数在 sleep , sleep 说明连接数没有被释放一直在被占用

 

 

 

分析思路

 

1 分析代码中可能有没有 未关闭的数据库连接

 

2 查看配置文件时候正确

 

 

 

代码中

 

如果代码中出现 Session session =  getSession(); 最后最好加上 session.close(); 这样就手动释放了这个连接,也可以换一种获取连接的方式

 

1
2
3
4
5
6
7
8
9
10
11
public  class  UniversalDao  extends  HibernateDaoSupport
{
   public  void  delete(Class clazz, Serializable id)
   {
     getHibernateTemplate().delete(get(clazz, id));
   }
 
   public  List findByCriteria(DetachedCriteria criteria) {
     return  getHibernateTemplate().findByCriteria(criteria);
   }
}

 

这样 getHibernateTemplate() 获取数据连接

 

 

 

配置文件中

 

1
2
3
4
5
6
<!-- 对于建立时间超过removeAbandonedTimeout的连接强制关闭 -->
<property name= "removeAbandoned"  value= "true"  /> <!-- 打开removeAbandoned功能 -->
<!--  180 秒,也就是 3 分钟 -->
<property name= "removeAbandonedTimeout"  value= "180"  /> <!-- 世界设置小  3 分钟sleep 直接强制关闭 -->
<!-- 关闭abanded连接时输出错误日志 -->
<property name= "logAbandoned"  value= "true"  />

 

  关联hibernate 配置中

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<bean id= "sessionFactory"  class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
         <property name= "dataSource"  ref= "dataSource"  />
         <property name= "hibernateProperties" >
             <props>
                 <prop key= "hibernate.dialect" > ${hibernate.dialect} </prop>
                 <prop key= "hibernate.show_sql" > ${hibernate.show_sql} </prop>
                 <prop key= "hibernate.jdbc.fetch_size" > ${hibernate.jdbc.fetch_size} </prop>
                 <prop key= "hibernate.jdbc.batch_size" > ${hibernate.jdbc.batch_size} </prop>
                 <prop key= "hibernate.format_sql" > ${hibernate.format_sql} </prop>
                 <prop key= "hibernate.c3p0.max_statements" > ${hibernate.c3p0.max_statements} </prop>
                 <prop key= "hibernate.hbm2ddl.auto" >none</prop>
                 <prop key= "hibernate.connection.release_mode" >after_transaction</prop>
             </props>
         </property>

 

 属性  key="hibernate.connection.release_mode" 这一列要填 after_transaction, 如果是auto也会很有可能初恋数据库连接一直活跃被占用

 

好了,这几个地方注意下,基本上是可以解决问题的

 

 

 

转载于:https://www.cnblogs.com/passby-zhang/p/10173559.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值