13

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?

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151

2 Answers2

17

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>
Cole W
  • 15,123
  • 6
  • 51
  • 85
4

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.

nathan gonzalez
  • 11,817
  • 4
  • 41
  • 57
  • I'm not sure I understand your 2nd point. Sessions are not thread safe so you have to start another session per thread. Also NHibernate uses connection pooling so open connections to the database are minimized already. Don't fix it if it isn't broke, imho. – Cole W Jun 16 '11 at 11:57
  • "Also regarding WCF integration: if you use ThreadStatic session context it will appear to work on development, but you will hit the wall in production when various components (ex: authorization, authentication ) from the wcf pipeline are executed on different threads." - According to this post: http://stackoverflow.com/a/5393312/878612 – lko Oct 23 '12 at 07:29