这是我写的获得Session和关闭Session的一个类
public class getHibernate {
private static SessionFactory factory;
static{
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}
public Session openSession(){
Session session = factory.openSession();
return session;
}
public void closeSession(Session session){
if(session != null){
session.close();
}
}
}
这是在HibernateUtil.java中定义的添加数据的方法
private static Session session;
static getHibernate gh = new getHibernate();
public static void savePlacard(Placard placard){
Transaction tx = null;
try{
session = gh.openSession();
tx = session.beginTransaction();
session.save(placard);
tx.commit();
gh.closeSession(session);
}catch(Exception e){
e.printStackTrace();
tx.rollback();
}
}
这是在jsp页面中用于添加数据的代码:
<%
Placard placard = new Placard(); //创建公告对象
placard.setContent(request.getParameter("content")); //设置公告内容
placard.setSubject(request.getParameter("subject")); //设置公告主题
java.sql.Date dateDate = (java.sql.Date)getTime.getDate(); //调用公共类获得当前时间
placard.setDDate(dateDate); //设置公告时间
String person =(String)session.getAttribute("username");//获得公告发布人
System.out.println(person);
placard.setPerson(person); //设置公告人
HibernateUtil.savePlacard(placard); //保存公告对象
%>
程序运行后,要添加的数据成功存进去了,但是控制台却有如下的异常(我在每次调用HibernateUtil.java时都是开启一个行新的Session,然后调用再关上它,为什么还会出现这样的异常呢?),请高手指点一下,谢谢了
org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.afterTransactionBegin(SessionImpl.java:1331)
at org.hibernate.jdbc.JDBCContext.afterTransactionBegin(JDBCContext.java:211)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:83)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.hibernate.util.HibernateUtil.findPlacardCount(HibernateUtil.java:141)
at org.apache.jsp.EveryDay.BBC.bbc_005findex_jsp._jspService(bbc_005findex_jsp.java:161)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.struts.filter.MyFilter.doFilter(MyFilter.java:23)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)