hibernate的criteria引起的程序卡死我们的一个小程序总是在open session的时候停止响应,而且非常规律得在
hibernate的criteria引起的程序卡死
我们的一个小程序总是在open session的时候停止响应,而且非常规律得在操作第四次时停止。查阅很多资料,发现下面这篇文章很有启发,摘录如下:
前几天用spring+hibernate+struts写了个增/删/改/查的例子。调试期间问题就来了,当查询结果翻页好几次就没N久没有响应了。最后控制报错。网上查了,它说数据库连接(池)问题。
出现错误如下:
<2007-9-30?下午12时03分03秒?CST> <Error> <WebLogicServer> <BEA-000337> <ExecuteThread: '13' for queue: 'weblogic.kernel.Default' has been busy for "901" seconds working on the request "Http Request: /admin/school.do", which is more than the configured time (StuckThreadMaxTime) of "600" seconds.>
先说下我配置
1.环境:
spring 2.0.6,hibernate 3.2.3,struts 1.2.9,oracle 10.2,weblogic 8.1.4
jdbc是ojdbc14.jar
2.连接池用DBCP
<bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">???
????<property?name="driverClassName"?value="${jdbc.driverClassName}"/>???
????<property?name="url"?value="${jdbc.url}"/>???
????<property?name="username"?value="${jdbc.username}"/>???
????<property?name="password"?value="${jdbc.password}"/>???
bean>???
3.分页方法(参考springside的),此类继承HibernateDaoSupport
public?Page?listByPage(Class?entityClass,?int?pageNo,?int?pageSize,?List?criterions,?List?orders)?{????
????Criteria?criteria?=?createCriteria(entityClass,?criterions);????
????CriteriaImpl?impl?=?(CriteriaImpl)?criteria;????
???
????//?先把Projection和OrderBy条件取出来,清空两者来执行Count操作????
????Projection?projection?=?impl.getProjection();????
????//获取总记录数????
????int?totalCount?=?((Integer)?criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();????
???
????????
????if(totalCount?<?1)?{????
????????return?new?Page();????
????}????
????????
????//加排序????
????if(orders?!=?null)?{????
????????for(int?i=0;?i?
????????????criteria.addOrder((Order)?orders.get(i));????
????????}????
????}????
????//原来的投影????
????criteria.setProjection(projection);????
????int?startIndex?=?Page.getStartOfPage(pageNo,?pageSize);????
????//取得结果????
????List?list?=?criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();????
????????
????return?new?Page(startIndex,?totalCount,?pageSize,?list);????
}????
???
public?Criteria?createCriteria(Class?entityClass,?List?criterions)?{????
????Criteria?criteria?=?getSession().createCriteria(entityClass);????
????if(criterions?!=?null)?{????
????????for(int?i=0;?i?
????????????criteria.add((Criterion)?criterions.get(i));????
????????}????
????}????
????????
????return?criteria;????
}???
另外,没有用OpenSessionInViewFilter,struts与spring的整合:DelegatingRequestProcessor、action path与bean name同名。
?
翻页不过10次,服务器就没响应了,最后出现上面的错误的了。
刚用weblogic,也刚用ssh套餐。郁闷。
前天解决了。
问题解决,问题的原因是数据库连接耗尽,我用HiberanteDaoSupport的getSession()方法取得Session后没有释放Session。
出问题的代码处(红色部分):
public?Criteria?createCriteria(Class?entityClass,?List?criterions)?{????????
????Criteria?criteria?=?getSession().createCriteria(entityClass);????????
????if(criterions?!=?null)?{????????
????????for(int?i=0;?i?????
????????????criteria.add((Criterion)?criterions.get(i));????????
????????}????????
????}????????
????????????
????return?criteria;????????
}???
用完Session释放后就没事了,调用HiberanteDaoSupport的releaseSession(session);方法后即可解决。
现在正确的代码:
public?Page?listByPage(Class?entityClass,?int?pageNo,?int?pageSize,?List?criterions,?List?orders)?{????
????Session?session?=?getSession();????
????//创建criteria????
????Criteria?criteria?=?session.createCriteria(entityClass);????
????//为criteria添加criterions????
????createCriteria(entityClass,?criteria,?criterions);????
????CriteriaImpl?impl?=?(CriteriaImpl)?criteria;????
???
????//?先把Projection和OrderBy条件取出来,清空两者来执行Count操作????
????Projection?projection?=?impl.getProjection();????
????//获取总记录数????
????int?totalCount?=?((Integer)?criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();????
???
????????
????if(totalCount?<?1)?{????
????????return?new?Page();????
????}????
????????
????//加排序????
????if(orders?!=?null)?{????
????????for(int?i=0;?i?
????????????criteria.addOrder((Order)?orders.get(i));????
????????}????
????}????
????//原来的投影????
????criteria.setProjection(projection);????
????int?startIndex?=?Page.getStartOfPage(pageNo,?pageSize);????
????//取得结果????
????List?list?=?criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();????
????//释放hiberante资源,一定要释放,要不然就数据库连接耗尽.????
????releaseSession(session);????
????return?new?Page(startIndex,?totalCount,?pageSize,?list);????
}????
???
public?Criteria?createCriteria(Class?entityClass,?Criteria?criteria,?List?criterions)?{????
????if(criterions?!=?null)?{????
????????for(int?i=0;?i?
????????????criteria.add((Criterion)?criterions.get(i));????
????????}????
????}????
????????
????return?criteria;????
} ??
?
?
?
?
?