Difference between CallSessionContext, ThreadLocalSessionContext and ThreadStaticSessionContext

From NHibernate documentation , this does not explain much.

What is the difference between the three?

In which situation is one of these contexts preferable to the other?

PS ThreadLocalSessionContext does not exist in the documentation, but it exists in the NHibernate DLL.

+6
source share
1 answer

Currently not using ThreadLocalSessionContext . There is no configuration that supports its use, and it only refers to unit test in NHibernate.

According to the code, CallSessionContext is a way to handle sessions in deleting .Net, see the comments in the code below for more details. It appears that NHibernate basically stores the session in the context of a remote call. More about remote call contexts can be found here.

 /// <summary> /// Provides a <see cref="ISessionFactory.GetCurrentSession()">current session</see> /// for each <see cref="System.Runtime.Remoting.Messaging.CallContext"/>. /// Not recommended for .NET 2.0 web applications. 

ThreadStaticSessionContext is used to handle sessions in multi-threaded applications. It uses the [ThreadStatic] attribute to declare a session so that there is a session in the thread. I am currently using this. See this SO link for sample code on how you use it: What is the best approach for managing an NHibernate session for use in a multi-threaded Windows application?

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

 /// <summary> /// Provides a <see cref="ISessionFactory.GetCurrentSession()">current session</see> /// for the current OperationContext in WCF. Works only during the lifetime of a WCF operation. /// </summary> 

To answer your second question, it really depends on what type of application you use and how you use your sessions. We hope that between the nhibernate documentation and the above descriptions, you can better understand which context you should use.

+7
source

All Articles