How to properly use a NHibernate ISession object - Session Is Closed! errors

前端 未结 4 1704
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 19:23

I\'m running into issues with my ISessions in NHibernate. I keep getting \"Session Closed!\" errors. Can some one please show me the correct pattern including a definition o

相关标签:
4条回答
  • 2021-01-04 19:33

    About the problem, your method of locking is right as long as you dispose the session but probably the bug lies under another part of your codes. by the way about the design, it is better that you pass the session variable to repositories because of unit of work implementation of the session and aggregate root's transaction like this:

    using (ISession session = SessionFactory.OpenSession())
    {
        Repository1 rep1 = new Repository1(session);
        Repository2 rep1 = new Repository2(session);
        Repository3 rep1 = new Repository3(session);
    
        // some logics
    
        using (var tx = session.BeginTransaction())
            tx.Commit();
    }
    

    . . .

    0 讨论(0)
  • 2021-01-04 19:44

    I advice you to read the documentation of ISession on https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/ISession.cs

    Anyway the proper way to clean up when you are finished with the session is to dispose it (or better, surround the usage with using statement). In this case, "using" closes the session and suppresses the finalizer, i.e. it prevents the session object from unnecessarily surviving the next garbage collecting and saves the memory.

    If the connection is already closed, disposing it will not throw an exception. On the other hand, closing after disposing (or after closing) throws an exception.

    The documentation recommends calling disconnect instead of closing, because this releases the connection to the connection pool. You should call Reconnect before using a disconnected session.

    For my needs, I always use "using" which calls Dispose and have never used the othe two functions.

    0 讨论(0)
  • 2021-01-04 19:47

    You should always use session.Dispose(); The other are for very strange occurances

    0 讨论(0)
  • 2021-01-04 19:51

    The issue lies in the fact the ISession is not thread-safe. There were multiple methods being fired on separate threads that all created an instance of ISession. The issue was really with the fact that they all shared the same SessionFactory. Image both of these methods are fired off on separate threads:

    ISessionFactory _sessionFactory;
    
    void MethodOne()
    {
       using(ISession session = _sessionFactory.OpenSession()) 
       {
           //Do something with really quick with the session
           //Then dispose of it
       }
    }
    
    void MethodTwo()
    {
       //OpenSession() actually returns the same instance used in the 
       //previous method that has already disposed of the object;
       using(ISession session = _sessionFactory.OpenSession()) 
       {
           //Do something with a session that has already been disposed
           //throws errors
    
       }
    }
    

    How I fixed it was basically ditching NHIbernate in these scenarios and called stored procs instead. I think it turned out to be more performant in my situation anyway.

    0 讨论(0)
提交回复
热议问题