2011-03-22 64 views
0

最近我做了一些工作來檢查在哪裏給我們的應用程序中的會話相關的代碼, ie .Getting current session(sessionFactory.getCurrentSession());爲什麼我們在應用程序中獲得兩次currentsession?

我看到這個代碼發生在應用程序兩次,一次在 HibernateSessionRequestFileter類

package com.persistence; 

import java.io.IOException; 

import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.hibernate.SessionFactory; 
import org.hibernate.StaleObjectStateException; 
public class HibernateSessionRequestFilter implements Filter { 

    private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class); 

    private SessionFactory sf; 

    public void doFilter(ServletRequest request, 
         ServletResponse response, 
         FilterChain chain) 
      throws IOException, ServletException { 

     try { 
      log.debug("Starting a database transaction"); 
      sf.getCurrentSession().beginTransaction(); 

      // Call the next filter (continue request processing) 
      chain.doFilter(request, response); 

      // Commit and cleanup 
      log.debug("Committing the database transaction"); 
      sf.getCurrentSession().getTransaction().commit(); 

     } catch (StaleObjectStateException staleEx) { 
      log.error("This interceptor does not implement optimistic concurrency control!"); 
      log.error("Your application will not work until you add compensation actions!"); 
      // Rollback, close everything, possibly compensate for any permanent changes 
      // during the conversation, and finally restart business conversation. Maybe 
      // give the user of the application a chance to merge some of his work with 
      // fresh data... what you do here depends on your applications design. 
      throw staleEx; 
     } catch (Throwable ex) { 
      // Rollback only 
      ex.printStackTrace(); 
      try { 
       if (sf.getCurrentSession().getTransaction().isActive()) { 
        log.debug("Trying to rollback database transaction after exception"); 
        //sf.getCurrentSession().getTransaction().rollback(); 
       } 
      } catch (Throwable rbEx) { 
       log.error("Could not rollback transaction after exception!", rbEx); 
      } 

      // Let others handle it... maybe another interceptor for exceptions? 
      throw new ServletException(ex); 
     } 
    } 

    public void init(FilterConfig filterConfig) throws ServletException { 
     log.debug("Initializing filter..."); 
     log.debug("Obtaining SessionFactory from static HibernateUtil singleton"); 
     sf = HibernateUtil.getSessionFactory(); 
    } 

    public void destroy() {} 


} 

另外一個在下面GenericHibernateDAO類一樣,

protected Session getSession() { 

     if (session == null) { 
      session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     } else if (!session.isConnected()) { 
      session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     } 
     return session; 
    } 

任何人可以幫助我瞭解,爲什麼我們必須在兩個地方進行激情? 當我們開始交易時,我們正在獲得當前會話,就像我們堅持或從數據庫獲取對象一樣,同樣我們正在獲得當前會話,爲什麼會這樣呢?

回答

2

這看起來像OpenSessionInView模式的版本,其中Hibernate Session在接收​​到請求時打開,在呈現響應後關閉。

在過濾器中打開一個會話並開始一個事務。

然後請求被處理,並在道調用getCurrentSession()只能得到是當前打開的會話,它不創建一個新的會話。

dao完成它的工作。 然後過濾器提交事務並關閉會話。

+0

私人會話會話; – Elavarasi 2011-03-24 07:01:11

+0

是的,我同意你的觀點,你的解釋,但我仍然不能區分 如何告訴下面的代碼只獲得通用dao中的當前會話和相同的代碼在過濾器類中獲得新的會話? session = HibernateUtil.getSessionFactory()。getCurrentSession(); – Elavarasi 2011-03-24 07:08:12

+0

請明確你不明白的地方。 – 2011-03-24 11:53:45

相關問題