本文介绍了NHibernate的在ASP,网络的Isession帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用NHibernate并asp.net MVC应用程序。

We're using nhibernate in and asp.net MVC application.

我们正在实现每个请求模式会话,通过一个HTTP模块。

We are implementing the Session per Request pattern, via a httpModule.

看起来pretty直线前进,但当我们与NHibernate探查运行,它清楚地表明,
会议永远不会得到休息。

It looks pretty straight forward, but when we run with NHibernate Profiler, it clearly shows that thesessions are never getting closed.

的模式似乎直线前进......但我不明白为什么会不会被关闭。

the pattern seems straight forward...but I don't understand why the sessions are never closing.

这里的code,我认为是非常重要的。

here's the code i think is important.

设置事件处理程序:

    context.EndRequest += new EventHandler(this.context_EndRequest);

在处理程序处理的会话

in the handler dispose the Session

private void context_EndRequest(object sender, EventArgs e)
        {
            netLogHdl.ArchDebug("NHibernateHttpModule.context_EndRequest() ");

            Dispose(0);// we are hitting 2 dbs and thus keep one session for each.
            Dispose(1);

            HttpContextBuildPolicy.DisposeAndClearAll();
        }


private void Dispose(int sessionIndex)
        {
            netLogHdl.ArchStart("NHibernateHttpModule.Dispose", "int sessionIndex=\" + sessionIndex + \")");

            try
            {
                //close the DB session
                string sessManagerName = "";
                string jcdcManager = "JCDC Manager";
                string spamisManager = "Spamis Manager";

                if (sessionIndex == 0)
                    sessManagerName = jcdcManager;
                else
                {
                    sessManagerName = spamisManager;
                }


                ISession oneSession = sessionPerDB[sessionIndex];
                if (oneSession != null)
                {
                    if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " oneSession is NOT null");

                    if (oneSession.IsOpen)
                    {
                        // Don't flush - all saves should use transactions and calling Commit does the flush.
                        if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Closing the session");

                        //This will overrite it with the exact same session, if they don't match something weird is going on - EWB
                        oneSession = CurrentSessionContext.Unbind(factoryPerDB[sessionIndex]);
                        oneSession.Close();
                    }
                    else
                    {
                        if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session is NOT open");
                    }

                    //if ( sessManagerName == jcdcManager ) netLogHdl.ArchDebug( sessManagerName + " Session got Dispose()-ing" );
                    //oneSession.Dispose();
                }
                else
                {
                    if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session is NULL");
                }

                sessionPerDB[sessionIndex] = null;

            }
            catch (Exception)
            {

                throw;
            }


            netLogHdl.ArchEnd();
        }

任何人都可以点我在正确的方向?我768,16看,是在花样不落实correclty?

Can anyone point me in the right direction? What shoud I look at, is the pattern not implemented correclty?

我狼狈不堪

谢谢!

电子 -

推荐答案

您应该调用处置,不要断开或关闭。 System.IDisposable的实施对象应该总是通过调用Dispose方法,或者通过使用块进行处理。

You should call dispose and not disconnect or close. System.IDisposable implementing objects should always be disposed by calling the dispose method, or by a using block.

这篇关于NHibernate的在ASP,网络的Isession帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:38