guoshilong
guoshilong
2008-07-22 14:06
浏览 162
已采纳

今天在开发网站时候,遇到异常问题

这是我的代码:
public class GroupDao implements IDao {
[color=darkred]Session session = HibernateSessionFactory.getSession();

Transaction tran = session.beginTransaction();[/color]  
     public void insert(Object obj) {

    Group group = (Group) obj;
    session.save(group);
    tran.commit();
    HibernateSessionFactory.closeSession();

}

public List queryAll() {
    String hql = "from Group";
    Query query = session.createQuery(hql);
    List list = query.list();
         HibernateSessionFactory.closeSession();
    return list;
}

}

jsp页面上报的错
exception

javax.servlet.ServletException:[color=red]Session is closed![/color] org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.school.QuanXianFilter.FilterEncoding.doFilter(FilterEncoding.java:23)

root cause

org.hibernate.SessionException: [color=red]Session is closed![/color] org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:526)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
com.school.QuanXianIDaoImpl.GroupDao.insert(GroupDao.java:26)
com.school.QuanXianAction.Action.addGroup(Action.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.school.QuanXianFilter.FilterEncoding.doFilter(FilterEncoding.java:23)
当我执行了插入以后,在返回页面,执行查询(queryAll())方法后,jsp页面却抛出了以上异常信息

研究了好长时间,可是没找出错误的原因。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • llade163
    llade163 2008-07-22 14:20
    已采纳

    Session怎么能作为成员变量呢?这样意味着你的GroupDao 对象是不可以重复使用的。第二次使用肯定会出Session is close的错误。因为你第一次使用GroupDao 时候Session已经关闭。

    点赞 评论
  • hglxr
    hglxr 2008-07-22 16:27

    public class GroupDao implements IDao {

    public void insert(Object obj) {
    Session session = HibernateSessionFactory.getSession();
    Transaction tran = session.beginTransaction();
    Group group = (Group) obj;
    session.save(group);
    tran.commit();
    HibernateSessionFactory.closeSession();

    }

    public List queryAll() {
    Session session = HibernateSessionFactory.getSession();
    String hql = "from Group";
    Query query = session.createQuery(hql);
    List list = query.list();
    HibernateSessionFactory.closeSession();
    return list;
    }
    }

    这样该没有问题了。
    你出现错误原因楼上已经说的很清楚了。

    点赞 评论

相关推荐