在多线程 Windows 服务应用程序中使用的最佳 NHibernate 会话管理方法是什么? [英] What is the best NHibernate session management approach for using in a multithread windows service application?
问题描述
我有一个使用多线程的 Windows 服务应用程序.我在这个应用的数据访问层使用了NHibernate.
I have a windows service application that work with multithread. I use NHibernate in data access layer of this application.
您对此应用程序中的会话管理有何建议.我读过 UNHAddins,这是一个好的解决方案吗?
What is your suggestion for session management in this application. I read about UNHAddins, Is it a good solution?
推荐答案
我使用 NHibernate 的内置上下文会话.您可以在此处阅读有关它们的信息:
I use NHibernate's built in contextual sessions. You can read about them here:
http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session
这是我如何使用它的示例:
Here is an example of how I use this:
public class SessionFactory
{
protected static ISessionFactory sessionFactory;
private static ILog log = LogManager.GetLogger(typeof(SessionFactory));
//Several functions omitted for brevity
public static ISession GetCurrentSession()
{
if(!CurrentSessionContext.HasBind(GetSessionFactory()))
CurrentSessionContext.Bind(GetSessionFactory().OpenSession());
return GetSessionFactory().GetCurrentSession();
}
public static void DisposeCurrentSession()
{
ISession currentSession = CurrentSessionContext.Unbind(GetSessionFactory());
currentSession.Close();
currentSession.Dispose();
}
}
除此之外,我的休眠配置文件中还有以下内容:
In addition to this I have the following in my hibernate config file:
<property name="current_session_context_class">thread_static</property>
这篇关于在多线程 Windows 服务应用程序中使用的最佳 NHibernate 会话管理方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!