What is the best NHibernate session management approach for using in a multithread windows service application?

倖福魔咒の 提交于 2019-11-27 01:02:45

问题


I have a windows service application that work with multithread. I use NHibernate in data access layer of this application.

What is your suggestion for session management in this application. I read about UNHAddins, Is it a good solution?


回答1:


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>



回答2:


i've not ever looked at the unhaddins, but here's what i use for wcf stuff, should probably work for multithreaded general stuff too i imagine.

this is the session context:

namespace Common.Infrastructure.WCF
{
    public class NHibernateWcfSessionContext : ICurrentSessionContext
    {
        private readonly ISessionFactoryImplementor factory;

        public NHibernateWcfSessionContext(ISessionFactoryImplementor factory)
        {
            this.factory = factory;
        }

        /// <summary>
        /// Retrieve the current session for the session factory.
        /// </summary>
        /// <returns></returns>
        public ISession CurrentSession()
        {
            Lazy<ISession> initializer;
            var currentSessionFactoryMap = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>().SessionFactoryMaps;
            if (currentSessionFactoryMap == null ||
                !currentSessionFactoryMap.TryGetValue(factory, out initializer))
            {
                return null;
            }
            return initializer.Value;
        }

        /// <summary>
        /// Bind a new sessionInitializer to the context of the sessionFactory.
        /// </summary>
        /// <param name="sessionInitializer"></param>
        /// <param name="sessionFactory"></param>
        public static void Bind(Lazy<ISession> sessionInitializer, ISessionFactory sessionFactory)
        {
            var map = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>().SessionFactoryMaps;;
            map[sessionFactory] = sessionInitializer;
        }

        /// <summary>
        /// Unbind the current session of the session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        /// <returns></returns>
        public static ISession UnBind(ISessionFactory sessionFactory)
        {
            var map = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>().SessionFactoryMaps;
            var sessionInitializer = map[sessionFactory];
            map[sessionFactory] = null;
            if (sessionInitializer == null || !sessionInitializer.IsValueCreated) return null;
            return sessionInitializer.Value;
        }
    }
}

this is the context manager :

namespace Common.Infrastructure.WCF
{
    class NHibernateContextManager : IExtension<InstanceContext>
    {
        public IDictionary<ISessionFactory, Lazy<ISession>> SessionFactoryMaps = new Dictionary<ISessionFactory, Lazy<ISession>>();

        public void Attach(InstanceContext owner)
        {
            //We have been attached to the Current operation context from the ServiceInstanceProvider
        }

        public void Detach(InstanceContext owner)
        {
        }
    }
}

Edit:

to be clear, as the other answer states, the thread static context will work out of the box. the main advantage of what i have here is 1) you get to be in control, and 2) its a lazy implementation, so you don't have to start a session for each thread if its not necessary. less connection to the db is always better, imho.



来源:https://stackoverflow.com/questions/6353692/what-is-the-best-nhibernate-session-management-approach-for-using-in-a-multithre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!