Nhibernate session management strategy for web application with background-workers?

被刻印的时光 ゝ 提交于 2019-12-19 03:38:09

问题


For a web application, it seems like a good way to handle the session is to use the setting <property name="current_session_context_class">managed_web</property>, call CurrentSessionContext.Bind/Unbind on Begin/EndRequest. Then I can just use sessionFactory.GetCurrentSession() in the repository class.

This works fine for all page request. But I have background workers doing stuff and using the same repository classes to do stuff. These do not run within a web request, so that session handling won't work.

Any suggestions to how this can be solved?


回答1:


I solved it by creating my own session context class:

public class HybridWebSessionContext : CurrentSessionContext
{
    private const string _itemsKey = "HybridWebSessionContext";
    [ThreadStatic] private static ISession _threadSession;

    // This constructor should be kept, otherwise NHibernate will fail to create an instance of this class.
    public HybridWebSessionContext(ISessionFactoryImplementor factory)
    {
    }

    protected override ISession Session
    {
        get
        {
            var currentContext = ReflectiveHttpContext.HttpContextCurrentGetter();
            if (currentContext != null)
            {
                var items = ReflectiveHttpContext.HttpContextItemsGetter(currentContext);
                var session = items[_itemsKey] as ISession;
                if (session != null)
                {
                    return session;
                }
            }

            return _threadSession;
        }
        set
        {
            var currentContext = ReflectiveHttpContext.HttpContextCurrentGetter();
            if (currentContext != null)
            {
                var items = ReflectiveHttpContext.HttpContextItemsGetter(currentContext);
                items[_itemsKey] = value;
                return;
            }

            _threadSession = value;
        }
    }
}



回答2:


I've found it simplest in this scenario to handle session creation myself using a DI library and 'hybrid' scope (in StructureMap, this is defined as InstanceScope.Hybrid). This will scope instances by HttpContext in an ASP.net app domain, and ThreadStatic in a normal app domain, allowing you to use the same approach in both.

I'm sure other DI libraries offer a similar feature.




回答3:


On my project, I wrote a little wrapper class around the CurrentSessionContext.
Perhaps you can extend it to suit your needs.
I think you just need to tweak the implementation of BindSessionToRequest and GetCurrentSession:

public static class SessionManager
    {
        private static ISessionFactory _sessionFactory = null;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    //check whether we're in web context or win context, and create the session factory accordingly.
                    if (System.Web.HttpContext.Current != null)
                    {
                        if (_sessionFactory == null)
                        {
                            _sessionFactory = DAOBase.GetSessionFactory();
                        }
                    }
                    else
                    {
                        _sessionFactory = DAOBase.GetSessionFactoryForWin();
                    }
                }
                return _sessionFactory;
            }
        }

        public static void BindSessionToRequest()
        {
            ISession session = SessionManager.SessionFactory.OpenSession();
            NHibernate.Context.CurrentSessionContext.Bind(session);
        }

        public static bool CurrentSessionExists()
        {
            return NHibernate.Context.CurrentSessionContext.HasBind(SessionFactory);
        }

        public static void UnbindSession()
        {
            ISession session = NHibernate.Context.CurrentSessionContext.Unbind(SessionManager.SessionFactory);
            if (session != null && session.IsOpen)
            {
                session.Close();
            }
        }

        public static ISession GetCurrentSession()
        {
            return SessionFactory.GetCurrentSession();
        }
    }


来源:https://stackoverflow.com/questions/5854238/nhibernate-session-management-strategy-for-web-application-with-background-worke

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