加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 每个会话的NH请求 – “会话已关闭!”

发布时间:2020-12-16 00:15:45 所属栏目:asp.Net 来源:网络整理
导读:NHibernate版本:2.1 我正在使用看起来非常标准的HttpModule方法来实现ASP.NET NHibernate应用程序中的每个请求会话.我正在尝试利用WebSessionContext,但它似乎没有正常工作.具体来说,对于应用程序上的第一个请求,一切都运行良好,但是其他请求会导致“会话关
NHibernate版本:2.1

我正在使用看起来非常标准的HttpModule方法来实现ASP.NET NHibernate应用程序中的每个请求会话.我正在尝试利用WebSessionContext,但它似乎没有正常工作.具体来说,对于应用程序上的第一个请求,一切都运行良好,但是其他请求会导致“会话关闭!”任何时候使用会话都是例外.重置应用程序池允许另一个请求成功,然后更多“会话关闭!”.

有一些活动的部分,但我不知道如何管理上下文以缩小它…所以这就是一切!

在web.config中:

<property name="current_session_context_class">
  NHibernate.Context.WebSessionContext,NHibernate
</property>

(我也尝试将它设置为’web’,结果相同.)

确认配置正确的模块:

public class NHibernateSessionModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        Debug.WriteLine("NHibernateSessionModule.Init()");
        context.BeginRequest += context_BeginRequest;
        context.EndRequest += context_EndRequest;
    }

    void context_BeginRequest(object sender,EventArgs e)
    {
        Debug.WriteLine("NHibernateSessionModule.BeginRequest()");
        var session = NHibernateHelper.OpenSession();
        session.BeginTransaction();
        CurrentSessionContext.Bind(session);
    }

    void context_EndRequest(object sender,EventArgs e)
    {
        Debug.WriteLine("NHibernateSessionModule.EndRequest()");
        var session = NHibernateHelper.GetCurrentSession();
        if (session != null)
        {
            try
            {
                if (session.Transaction != null && session.Transaction.IsActive)
                    session.Transaction.Commit();
            }
            catch (Exception ex)
            {
                session.Transaction.Rollback();
                throw new ApplicationException("Error committing database transaction",ex);
            }
            finally
            {
                session.Close();
            }
        }
        CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
    }
}

还有我的小帮手:

public class NHibernateHelper
{
    public static readonly ISessionFactory SessionFactory;

    static NHibernateHelper()
    {
        try
        {
            Configuration cfg = new Configuration();
            cfg.AddAssembly(Assembly.GetCallingAssembly());
            SessionFactory = cfg.Configure().BuildSessionFactory();
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
            throw new ApplicationException("NHibernate initialization failed",ex);
        }
    }

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

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

解决方法

NHibernate 1.2(来自NHibernate in Action)的示例显示unbind在关闭之前完成.

订购的这种变化有帮助吗?

var session = NHibernateHelper.GetCurrentSession();
CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
...
session.Close();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读