问题描述
来自 NHibernate文档,它没有解释太多.
From NHibernate documentation, it doesn't explain much.
这三个有什么区别?
在什么情况下这些上下文中的一个比其他上下文更可取?
In what situation that one of those context is more preferable than the others ?
附言ThreadLocalSessionContext
在文档中不存在,但在 NHibernate dll 中确实存在.
P.S. ThreadLocalSessionContext
doesn't exist in the documentation, but it does exist in NHibernate dll.
推荐答案
目前看来 ThreadLocalSessionContext 无法使用.没有支持它使用的配置,它仅在 NHibernate 中通过单元测试引用.
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.
根据代码 CallSessionContext 是一种在 .Net 远程处理中处理会话的方法,请参阅下面代码中的注释以了解更多详细信息.看起来 NHibernate 基本上将会话存储在远程调用上下文中.可以在 此处找到有关远程处理调用上下文的更多信息
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
/// <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 用于处理多线程应用程序中的会话.它使用 [ThreadStatic] 属性来声明会话,以便每个线程都有一个会话.我目前使用这个.有关如何使用它的代码示例,请参阅此 SO 链接:在多线程 Windows 服务应用程序中使用的最佳 NHibernate 会话管理方法是什么?
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?
除此之外,NHibernate 似乎还在 3.2 版中添加了另一个名为 WcfOperationSessionContext 的会话上下文.以下是代码的描述:
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:
/// <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>
要回答您的第二个问题,这实际上取决于您正在实施的应用程序类型以及您如何使用会话.希望通过 nhibernate 文档和上面的描述,您对应该使用的上下文有更好的了解.
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.
这篇关于CallSessionContext、ThreadLocalSessionContext 和 ThreadStaticSessionContext 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!