关于数据库连接的一个郁闷错误

        废话不多说,最近在玩自己写的项目,框架是SSH2,数据库是用c3p0连接的,在给一个简单的查询加上分页之后,诡异的事情发生了:在多点几次查询操作时,后台报错:

数据库连接不上。

org.hibernate.exception.GenericJDBCException: Cannot open connection

。。。。

Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.

。。。。

Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@1240739 -- timeout at awaitAvailable()

。。。。

一开始怀疑是c3p0设置问题,在增大<property name="maxPoolSize"><value>20</value></property>值后 从原来的点7次变成点十几次后报异常。从网上找各种资料后并没有结果。最后在检查后台代码是发现了错误:

@Override
	@SuppressWarnings("all")
	public List<HouseSources> queryHouseSources(HouseSources hs,Pager pager, Map map,String hql){
		
		List<HouseSources> result = null;
		try{
			//这里使用了session,但是没用关闭,造成连接池溢出
		Query query = this.getSession().createQuery(hql); 
		 Iterator it = map.keySet().iterator();
		 while (it.hasNext())  
	        {  
	            Object key = it.next();  
	            query.setParameter(key.toString(), map.get(key));  
	        }  	  
	        query.setFirstResult((pager.getPageNo()- 1)*pager.getLimit());  
	        query.setMaxResults(pager.getLimit());  	  
	        result = query.list(); 
		}catch (Exception e){
			e.printStackTrace();
		}
	        return result ;
	}

这段代码是参考这里 http://gaogengzhi.iteye.com/blog/266301 的分页查询。

 this.getSession().createQuery(hql); 
这样的写法,不能自动关闭数据库连接,多次连接会溢出,所以一定要在使用后手动关闭,或者调用回调机制,让spring协助关闭修改后的写法如下:

	@Override
	@SuppressWarnings("all")
	public List<HouseSources> queryHouseSources(HouseSources hs,final Pager pager, final Map map,final String hql){
		
		return (List<HouseSources>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session) 
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				List<HouseSources> result = null;
				try{
				Query query = session.createQuery(hql); 
				 Iterator it = map.keySet().iterator();
				 while (it.hasNext())  
			        {  
			            Object key = it.next();  
			            query.setParameter(key.toString(), map.get(key));  
			        }  	 				
			        query.setFirstResult((pager.getPageNo()- 1)*pager.getLimit());  
			        query.setMaxResults(pager.getLimit());  	  
			        result = query.list(); 
				}catch (Exception e){
					e.printStackTrace();
				}
			        return result ;				
			}			
		});	
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值