来自 NHibernate documentation ,不解释太多。
这三者有什么区别?
在什么情况下,这些上下文中的一个比其他上下文更可取?
附言ThreadLocalSessionContext
文档中不存在,但在 NHibernate dll 中确实存在。
最佳答案
看起来不像 ThreadLocalSessionContext 目前可以使用。没有支持它使用的配置,它仅在 NHibernate 中由单元测试引用。
根据代码 CallSessionContext 是一种在 .Net 远程处理中处理 session 的方法,请参阅下面代码中的注释以获取更多详细信息。看起来 NHibernate 基本上将 session 存储在远程调用上下文中。有关远程调用上下文的更多信息,请访问 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.
线程静态 session 上下文 用于处理多线程应用程序中的 session 。它使用 [ThreadStatic] 属性来声明 session ,以便每个线程都有一个 session 。我目前使用这个。有关如何使用它的代码示例,请参阅此 SO 链接:What is the best NHibernate session management approach for using in a multithread windows service application?
除此之外,看起来 NHibernate 在 3.2 版中添加了另一个名为 的 session 上下文。 WcfOperationSessionContext .以下是代码中的描述:
/// <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>
要回答您的第二个问题,它实际上取决于您正在实现的应用程序类型以及您如何使用 session 。希望在 nhibernate 文档和上面的描述之间,您对应该使用的上下文有更好的了解。
关于nhibernate - CallSessionContext、ThreadLocalSessionContext 和 ThreadStaticSessionContext 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6653366/