Why does Nhibernate share the session across multiple requests in my MVC application?

前端 未结 1 1952
一个人的身影
一个人的身影 2021-01-19 13:51

We have an MVC project that constructs the NHibernate dependecies via StructureMap like this

var sessionFactory = ConnectionRegistry.CreateSessionFactory<         


        
相关标签:
1条回答
  • 2021-01-19 14:06

    The problem is combination of a singleton:

    For<INHibernateSessionManager>().Singleton().Use<NHibernateWebSessionManager>();
    

    Having reference to an object from a different scope (webrequest context)

    _currentSession = SessionFactory.GetCurrentSession();
    

    This canot work properly in multithread environment (as mentioned in cases of two concurrent browsers accessing it). First request could already force to set the field _currentSession, which is then (for a while) used even for the second one. The first Application_EndRequest will close it ... and lasting one will recreate it...

    When relying on NHibernate scopes, follow it fully:

    return SessionFactory.GetCurrentSession(); // inside is the scope handled
    

    SessionManager.Open()

    public ISession OpenSession()
    {
      if(CurrentSessionContext.HasBind(SessionFactory))
      {
         return SessionFactory.GetCurrentSession();
      }
      // else  
      var session = SessionFactory.OpenSession();
      NHibernate.Context.CurrentSessionContext.Bind(session);
      return session;
    }
    

    Then even singleton returning correct instances should work. But for a SessionManager I would use HybridHttpOrThreadLocalScoped anyway.

    For<INHibernateSessionManager>()
      .HybridHttpOrThreadLocalScoped()
      .Use<NHibernateWebSessionManager>();
    
    0 讨论(0)
提交回复
热议问题