处理session封闭时再调用目标办法时报session已封闭疑问

session关闭往后,如果还想用相关的方针,比如在页面中进行输出,但又不至于出现session已关闭的差错而致使无法获取方针的信息,本文供应三种处置办法: 1:第一次获得该方针的时分用get而不用load,因get不支持懒加载,在get该方针的一同会想数据库宣告sql语句,取出该方针的相应信息放入缓存中,下次即使session现已关闭,因缓存中有相应的数据,查找该方针时会首要从缓存中查询,直接拿 http://www.goodk123.info 来用就ok了 2:可以把相应方针的lazy设为false,这样在load该方针的一同就会发sql语句取出该方针放到缓存中,往后再用可以直接从缓存中取,然后也避免了该失常 3:选用openSessionInView 该办法是通过延伸session的时辰办法来处置的,可以写一个filter或许inteceptor,下面以filter为例  private SessionFactory sf;
//filter   -- > 
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  try {
   // sf.getCurrentSession();//获得和其时线程绑定的session
   sf.getCurrentSession().beginTransaction();
   chain.doFilter(request, response);
   sf.getCurrentSession().getTransaction().commit();
  } catch (StaleObjectStateException staleEx) {
   throw staleEx;
  } catch (Throwable ex) {
   ex.printStackTrace();
   try {
    if (sf.getCurrentSession().getTransaction().isActive()) {
     sf.getCurrentSession().getTransaction().rollback();
    }
   } catch (Throwable rbEx) {
   }
   throw new ServletException(ex);
  }
 }
 public void init(FilterConfig filterConfig) throws ServletException {
  sf = HibernateSessionFactory.getSessionFactory();
 }
 public void destroy() {
 } http://www.gookp11.com 
posted @ 2013-04-10 05:36  chinadiy197601  阅读(215)  评论(0编辑  收藏  举报