“会话已结束!” -NHibernate

发布于 2024-08-05 08:09:09 字数 1558 浏览 3 评论 0原文

这是在 Web 应用程序环境中:

初始请求能够成功完成,但是任何其他请求都会从 NHibernate 框架返回“会话已关闭”响应。我使用带有以下代码的 HttpModule 方法:

public class MyHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += ApplicationEndRequest;
        context.BeginRequest += ApplicationBeginRequest;
    }

    public void ApplicationBeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(SessionFactory.Instance.OpenSession());
    }

    public void ApplicationEndRequest(object sender, EventArgs e)
    {
        ISession currentSession = CurrentSessionContext.Unbind(
            SessionFactory.Instance);

        currentSession.Dispose();
    }

    public void Dispose() { }
}

SessionFactory.Instance 是我的单例实现,使用 FluentNHibernate 返回 ISessionFactory 对象。

在我的存储库类中,我尝试使用以下语法:

public class MyObjectRepository : IMyObjectRepository
{
    public MyObject GetByID(int id)
    {
        using (ISession session = SessionFactory.Instance.GetCurrentSession())
            return session.Get<MyObject>(id);
    }
}

这允许应用程序中的代码这样调用:

IMyObjectRepository repo = new MyObjectRepository();
MyObject obj = repo.GetByID(1);

我怀疑我的存储库代码是罪魁祸首,但我不能 100% 确定我应该执行的实际实现正在使用。

我在此处上发现了类似的问题。我也在我的实现中使用 WebSessionContext,但是,除了编写自定义 SessionManager 之外,没有提供任何解决方案。对于简单的 CRUD 操作,除了内置工具(即 WebSessionContext)之外,是否还需要自定义会话提供程序?

This is in a web application environment:

An initial request is able to successfully complete, however any additional requests return a "Session is Closed" response from the NHibernate framework. I'm using a HttpModule approach with the following code:

public class MyHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += ApplicationEndRequest;
        context.BeginRequest += ApplicationBeginRequest;
    }

    public void ApplicationBeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(SessionFactory.Instance.OpenSession());
    }

    public void ApplicationEndRequest(object sender, EventArgs e)
    {
        ISession currentSession = CurrentSessionContext.Unbind(
            SessionFactory.Instance);

        currentSession.Dispose();
    }

    public void Dispose() { }
}

SessionFactory.Instance is my singleton implementation, using FluentNHibernate to return an ISessionFactory object.

In my repository class, I attempt to use the following syntax:

public class MyObjectRepository : IMyObjectRepository
{
    public MyObject GetByID(int id)
    {
        using (ISession session = SessionFactory.Instance.GetCurrentSession())
            return session.Get<MyObject>(id);
    }
}

This allows code in the application to be called as such:

IMyObjectRepository repo = new MyObjectRepository();
MyObject obj = repo.GetByID(1);

I have a suspicion my repository code is to blame, but I'm not 100% sure on the actual implementation I should be using.

I found a similar issue on SO here. I too am using WebSessionContext in my implementation, however, no solution was provided other than writing a custom SessionManager. For simple CRUD operations, is a custom session provider required apart from the built in tools(ie WebSessionContext)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追星践月 2024-08-12 08:09:09

我还没有测试你的代码,但从阅读来看,这一行:

using (ISession session = SessionFactory.Instance.GetCurrentSession())

在块退出后转储你的会话,然后会话在下次通过时被处置/无效。

这是我们在应用程序中使用的模型:

ISession session = null;

try
{
    // Creates a new session, or reconnects a disconnected session
    session = AcquireCurrentSession();

    // Database operations go here
}
catch
{
    session.Close();
    throw;
}
finally
{
    session.Disconnect();
}

I haven't tested your code, but from reading, this line:

using (ISession session = SessionFactory.Instance.GetCurrentSession())

is dumping your session after the block exits, and then the session is disposed/invalid on the next time through.

Here's the model we're using in our applications:

ISession session = null;

try
{
    // Creates a new session, or reconnects a disconnected session
    session = AcquireCurrentSession();

    // Database operations go here
}
catch
{
    session.Close();
    throw;
}
finally
{
    session.Disconnect();
}
远昼 2024-08-12 08:09:09

我遇到了类似的错误。事实证明,我是在“新建”我的存储库,而不是让我的 IOC 容器解决它。

I got a similar error. Turns out I was "new"ing up my repository instead of having my IOC container resolve it.

2024-08-12 08:09:09

使用以下语句会在每次查询后处理或关闭会话:

using (ISession session = SessionFactory.Instance.GetCurrentSession())

而是在不使用“using”一词的情况下使用它:

ISession session = SessionFactory.Instance.GetCurrentSession()

这对我有用。

Use of the following statement disposes off or closes the session after every query:

using (ISession session = SessionFactory.Instance.GetCurrentSession())

Instead use it without the "using" word as:

ISession session = SessionFactory.Instance.GetCurrentSession()

This worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文