2009-12-02 63 views
1

我們在nhibernate和asp.net MVC應用程序中使用。在asp,net中的Nhibernate ISession的幫助

我們通過httpModule實現Session per Request模式。

它看起來非常簡單,但是當我們使用NHibernate Profiler運行時,它清楚地表明 會話永遠不會關閉。

該模式似乎很簡單......但我不明白爲什麼會議永遠不會結束。

這裏是我認爲很重要的代碼。

成立了事件處理程序:

context.EndRequest += new EventHandler(this.context_EndRequest); 

在處理程序處理的會話

private void context_EndRequest(object sender, EventArgs e) 
     { 
      netLogHdl.ArchDebug("NHibernateHttpModule.context_EndRequest() "); 

      Dispose(0);// we are hitting 2 dbs and thus keep one session for each. 
      Dispose(1); 

      HttpContextBuildPolicy.DisposeAndClearAll(); 
     } 


private void Dispose(int sessionIndex) 
     { 
      netLogHdl.ArchStart("NHibernateHttpModule.Dispose", "int sessionIndex=\" + sessionIndex + \")"); 

      try 
      { 
       //close the DB session 
       string sessManagerName = ""; 
       string jcdcManager = "JCDC Manager"; 
       string spamisManager = "Spamis Manager"; 

       if (sessionIndex == 0) 
        sessManagerName = jcdcManager; 
       else 
       { 
        sessManagerName = spamisManager; 
       } 


       ISession oneSession = sessionPerDB[sessionIndex]; 
       if (oneSession != null) 
       { 
        if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " oneSession is NOT null"); 

        if (oneSession.IsOpen) 
        { 
         // Don't flush - all saves should use transactions and calling Commit does the flush. 
         if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Closing the session"); 

         //This will overrite it with the exact same session, if they don't match something weird is going on - EWB 
         oneSession = CurrentSessionContext.Unbind(factoryPerDB[sessionIndex]); 
         oneSession.Close(); 
        } 
        else 
        { 
         if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session is NOT open"); 
        } 

        //if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session got Dispose()-ing"); 
        //oneSession.Dispose(); 
       } 
       else 
       { 
        if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session is NULL"); 
       } 

       sessionPerDB[sessionIndex] = null; 

      } 
      catch (Exception) 
      { 

       throw; 
      } 


      netLogHdl.ArchEnd(); 
     } 

任何人都可以點我在正確的方向?我看什麼,是沒有實現correcty的模式?

我是flummoxed

謝謝!

E-

+0

在上面的代碼的評論中,它說:「不要刷新 - 所有的保存應該使用交易和調用提交沖洗。」 「,但代碼既不刷新也不使用交易。我認爲這是一個問題。 – 2009-12-02 19:39:33

+0

交易與會話不同,他可能在http模塊之外處理它,就像我一樣。它是會話每請求,而不是每個請求事務。 – epitka 2009-12-02 20:07:47

+0

是的事務是在這個圖層之外提交的。 我做了jsut添加一個刷新,就在關閉之前incase會話was'nt使用交易由於某種原因(即獲取) – 2009-12-02 20:58:57

回答

1

你應該叫部署和未斷開或閉合。 System.IDisposable實現對象應始終通過調用dispose方法或使用塊進行處理。

+0

我現在正在做皮帶和吊帶方法 oneSession = CurrentSessionContext.Unbind(factoryPerDB [sessionIndex ]); oneSession.Flush(); oneSession.Flush(); oneSession.Close(); oneSession.Dispose(); 沒有快樂,我仍然有永恆的會議。 – 2009-12-02 21:06:20

+0

比錯誤是在我不知道的代碼部分的某處。 – Paco 2009-12-02 22:08:30