前几天用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.]
<转为[,>转为],才发得出以上面错误。可能是javaeye的bug。
先说下我配置
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
xml 代码
- <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
java 代码
- public Page listByPage(Class entityClass, int pageNo, int pageSize, List criterions, List orders) {
- Criteria criteria = createCriteria(entityClass, criterions);
- CriteriaImpl impl = (CriteriaImpl) criteria;
-
-
- 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次,服务器就没响应了,最后出现上面的错误的了。
问题解决,问题的原因是数据库连接耗尽,我用HiberanteDaoSupport的getSession()方法取得Session后没有释放Session。
出问题的代码处:
java 代码
- 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);方法后即可解决。
现在正确的代码:
java 代码
- public Page listByPage(Class entityClass, int pageNo, int pageSize, List criterions, List orders) {
- Session session = getSession();
-
- Criteria criteria = session.createCriteria(entityClass);
-
- createCriteria(entityClass, criteria, criterions);
- CriteriaImpl impl = (CriteriaImpl) criteria;
-
-
- 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<orders.size(); i++) {
- criteria.addOrder((Order) orders.get(i));
- }
- }
-
- criteria.setProjection(projection);
- int startIndex = Page.getStartOfPage(pageNo, pageSize);
-
- List list = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
-
- 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<criterions.size(); i++) {
- criteria.add((Criterion) criterions.get(i));
- }
- }
-
- return criteria;
- }