在多线程Windows服务应用程序中使用的最佳NHibernate会话管理方法是什么?

mas*_*ani 14 c# nhibernate windows-services session-management unhaddins

我有一个使用多线程的Windows服务应用程序.我在这个应用程序的数据访问层使用NHibernate.

您对此应用程序中的会话管理有何建议?我读到了UNHAddins,这是一个很好的解决方案吗?

Col*_*e W 17

我使用NHibernate内置的上下文会话.你可以在这里阅读它们:

http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session

这是我如何使用它的一个例子:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

除此之外,我在hibernate配置文件中有以下内容:

<property name="current_session_context_class">thread_static</property>
Run Code Online (Sandbox Code Playgroud)