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("响应回去");
[解决办法]