我有一个Coupons数据库,使用Hibernate我试图获得5行。
这是代码:
public List<Coupon> getPage(int pageNum, boolean isActive) {
session = factory.openSession();
session.beginTransaction();
Query query;
if(isActive) {
query = session.createQuery("from Coupon where (NOW() < fadeoutDate)");
} else {
query = session.createQuery("from Coupon");
}
query.setFirstResult((pageNum - 1) * numCouponsPerPage);
query.setMaxResults(numCouponsPerPage);
List<Coupon> page = (List<Coupon>) query.list();
closeSession();
return page;
}
(numCouponsPerPage是一个等于5的常量)
尝试获取页面时:
List<Coupon> p = cm.getPage(dpage,active.equalsIgnoreCase("true"));
(dpage = 1,第二个参数返回false
),
我收到了这个错误:
Dec 14, 2013 4:11:06 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/firstXDw] threw exception [An exception occurred processing JSP page /views/system/loadPage.jsp at line 30
27:
28: if (whatToLoad.equalsIgnoreCase("coupons")) {
29:
30: List<Coupon> p = cm.getPage(dpage,active.equalsIgnoreCase("true"));
31: for (Coupon c : p) {
32: out.println("<tr>");
33: out.println("<td>" + c.getId() + "</td>");
Stacktrace:] with root cause
org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1768)
at implementations.Coupons.getPage(Coupons.java:130)
at org.apache.jsp.views.system.loadPage_jsp._jspService(loadPage_jsp.java:82)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
为什么抛出异常Session is closed
?我在函数的第一行打开会话..
问题是什么以及如何解决?