Difference between CallSessionContext, ThreadLocalSessionContext and ThreadStaticSessionContext

后端 未结 1 1236
有刺的猬
有刺的猬 2021-01-13 00:35

From NHibernate documentation, it doesn\'t explain much.

What is the difference between those three ?

In what situation that one of those context is more pre

1条回答
  •  有刺的猬
    2021-01-13 00:44

    It doesn't look like ThreadLocalSessionContext can currently be used. There is no configuration that supports it's use and it's only referenced within NHibernate by a unit test.

    According to the code CallSessionContext is a way of handling sessions in .Net remoting see comments in code below for more detail. It looks like NHibernate basically stores the session in the remoting call context. More info about remoting call contexts can be found here

    /// 
    /// Provides a current session
    /// for each .
    /// Not recommended for .NET 2.0 web applications.
    

    ThreadStaticSessionContext is used for handling sessions in multi-threaded applications. It uses a [ThreadStatic] attribute to declare the session such that there is a session per thread. I currently use this. See this SO link for a code example of how you would use it: What is the best NHibernate session management approach for using in a multithread windows service application?

    Also in addition to this it looks like NHibernate is adding another session context in version 3.2 called WcfOperationSessionContext. Below is the description from the code:

    /// 
    /// Provides a current session
    /// for the current OperationContext in WCF. Works only during the lifetime of a WCF operation.
    /// 
    

    To answer your 2nd question it really depends on what type of application you are implementing and how you are using your sessions. Hopefully between the nhibernate documentation and the descriptions above you'll have a better idea of what context you should use.

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