NH要求每学期 - "会话关闭"! [英] NH Request Per Session - "Session is closed!"

查看:178
本文介绍了NH要求每学期 - "会话关闭"!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NHibernate的版本: 2.1

我用什么似乎是一个pretty标准的HttpModule方法在ASP.NET + NHibernate的应用程序中实现每个请求的会话。我想利用 WebSessionContext ,但它似乎并没有正常工作。具体来说,一切工作出色的应用程序的第一个请求,但其他请求导致一个会话被关闭!异常使用的会话的任何时间。重置应用程序池允许成功另一个请求,那么更多的会话被关闭!

有几件运动,但我不知道有足够的了解的背景下是如何设法缩小它所以......这里的一切!

在web.config中:

 <属性名=current_session_context_class>
  NHibernate.Context.WebSessionContext,NHibernate的
< /性>

(我试过将其设置为只是网络,也具有相同的结果。)

模块,确认正确配置:

 公共类NHibernateSessionModule:IHttpModule的
{
    公共无效的Dispose(){}    公共无效初始化(HttpApplication的情况下)
    {
        的Debug.WriteLine(NHibernateSessionModule.Init());
        context.BeginRequest + = context_BeginRequest;
        context.EndRequest + = context_EndRequest;
    }    无效context_BeginRequest(对象发件人,EventArgs的发送)
    {
        的Debug.WriteLine(NHibernateSessionModule.BeginRequest());
        VAR会话= NHibernateHelper.OpenSession();
        session.BeginTransaction();
        CurrentSessionContext.Bind(会话);
    }    无效context_EndRequest(对象发件人,EventArgs的发送)
    {
        的Debug.WriteLine(NHibernateSessionModule.EndRequest());
        VAR会话= NHibernateHelper.GetCurrentSession();
        如果(会话!= NULL)
        {
            尝试
            {
                如果(session.Transaction = NULL&放大器;!&安培; session.Transaction.IsActive)
                    session.Transaction.Commit();
            }
            赶上(异常前)
            {
                session.Transaction.Rollback();
                抛出新ApplicationException的(错误提交数据库事务,前);
            }
            最后
            {
                session.Close();
            }
        }
        CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
    }
}

和我的小帮手:

 公共类NHibernateHelper
{
    公共静态只读ISessionFactory SessionFactory的;    静态NHibernateHelper()
    {
        尝试
        {
            配置CFG =新配置();
            cfg.AddAssembly(Assembly.GetCallingAssembly());
            SessionFactory的= cfg.Configure()BuildSessionFactory()。
        }
        赶上(异常前)
        {
            的Debug.WriteLine(除息);
            抛出新ApplicationException的(NHibernate的初始化失败,前);
        }
    }    公共静态的ISession的getCurrentSession()
    {
        返回SessionFactory.getCurrentSession()函数;
    }    公共静态的ISession的openSession()
    {
        返回SessionFactory.OpenSession();
    }
}


解决方案

有关NHibernate的1.2例子(从NHibernate的行动),表明取消绑定在收盘前完成。

是否订购帮助的这种变化?

  VAR会话= NHibernateHelper.GetCurrentSession();
CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
...
session.Close();

NHibernate Version: 2.1

I'm using what seems to be a pretty standard HttpModule approach to implementing per-request sessions in an ASP.NET+NHibernate application. I'm trying to leverage WebSessionContext, but it doesn't seem to be working correctly. Specifically, everything works brilliantly for the first request on the application, but additional requests result in a "Session is closed!" exception any time the session is used. Resetting the application pool allows another request to succeed, then more "Session is closed!".

There are a few moving pieces, but I don't know enough about how the context is managed to narrow it down so...here's everything!

In web.config:

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

(I've tried setting it to just 'web', too, with the same result.)

The module, confirmed to be configured correctly:

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);
    }
}

And my little helper:

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();
    }
}

解决方案

The example for NHibernate 1.2 (from NHibernate in Action), shows that the unbind is done before the close.

Does this change of ordering help?

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

这篇关于NH要求每学期 - &QUOT;会话关闭&QUOT;!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆