首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nhibernate in asp,net ISession帮助

Nhibernate in asp,net ISession帮助
EN

Stack Overflow用户
提问于 2009-12-03 03:28:15
回答 2查看 489关注 0票数 1

我们在asp.net MVC应用程序中使用了nhibernate。

我们通过httpModule实现了每个请求的会话模式。

它看起来非常简单,但当我们使用NHibernate事件探查器运行时,它清楚地表明会话永远不会关闭。

模式似乎是直接的forward...but我不明白为什么会话永远不会结束。

下面是我认为重要的代码。

设置事件处理程序:

代码语言:javascript
复制
    context.EndRequest += new EventHandler(this.context_EndRequest);

在处理程序中处理会话

代码语言:javascript
复制
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();
        }

有谁能给我指个方向吗?我应该看什么,模式实现不正确吗?

我很迷惑

谢谢!

E-

EN

回答 2

Stack Overflow用户

发布于 2009-12-03 03:58:20

您应该调用dispose,而不是断开连接或关闭。System.IDisposable实现对象应始终通过调用dispose方法或通过using块来释放。

票数 1
EN

Stack Overflow用户

发布于 2009-12-03 07:24:14

您可能还想看看Ben Day's blog post上有关会话管理的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1835244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档