什么是使用多线程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:
<一个href=\"http://www.nhforge.org/doc/nh/en/index.html#architecture-current-session\">http://www.nhforge.org/doc/nh/en/index.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();
}
}
除了这个,我有以下我的hibernate配置文件:
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屋!