首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

session关闭的有关问题

2011-12-15 
session关闭的问题public Fwxx select(Class c, int fwid){Session sessionHibernateSessionFactory.getS

session关闭的问题
public Fwxx select(Class c, int fwid){
Session session=HibernateSessionFactory.getSession();
Fwxx fwxx=(Fwxx)session.get(c,fwid);
System.out.println(fwxx.getLxr());
HibernateSessionFactory.closeSession();
return fwxx;

}

我查询完之后就关闭session,然后在action调用这个方法后报
could not initialize proxy - the owning Session was closed

[解决办法]
这个可以把session的开关放在 过滤器里面 就不会出现session关闭的问题了

Java code
public void doFilter(ServletRequest arg0, ServletResponse arg1,            FilterChain arg2) throws IOException, ServletException {                Session session=null;        Transaction trans=null;        try{            session=HibernateSessionFactory.getSession();            trans=session.beginTransaction();            arg2.doFilter(arg0, arg1);            trans.commit();        }catch(Exception e){            e.printStackTrace();            if(trans!=null)                trans.rollback();        }finally{            HibernateSessionFactory.closeSession();        }        System.out.println("响应回去");
[解决办法]
探讨
session.removeAttribute("");
session.invalidate(); 是将session设置为失效

热点排行