NHibernate:获取所有打开的会话

发布于 2024-12-11 08:47:15 字数 247 浏览 0 评论 0原文

我有一个使用 NHibernate 的 ASP.NET 应用程序,由于某种原因,很少有开发人员忘记关闭某些页面中的会话(我认为是 20 个页面),我知道最好的解决方案是浏览每个页面并确保会话正确关闭,但我不能做这种动作,因为代码已经投入生产。因此,我试图找到一种方法来获取会话工厂中所有打开的会话,然后使用母版页或使用附加进程将其关闭,但我找不到方法来做到这一点。

那么,有没有办法获取所有打开的会话呢?或者也许设置会话空闲超时或其他什么,你有什么建议?谢谢指教。

I have an ASP.NET application with NHibernate, for some reason few developers forgot to close the sessions in some pages (like 20 I think), I know that the best solution is to go through each page and make sure the sessions are closed properly, but I can't do that kind of movement because the code is already on production. So I was trying to find a way to get all the opened sessions in the session factory and then close it using the master page or using an additional process but I can't find a way to do that.

So, is there a way to get all the opened sessions? or maybe set the session idle timeout or something, what do you suggest?. Thanks in advice.

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

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

发布评论

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

评论(1

萧瑟寒风 2024-12-18 08:47:15

据我所知,不支持从会话工厂获取打开的会话列表。我有自己的方法来监视打开的会话,并且使用以下结构:

创建一个对 ISession 弱引用的类。这样,如果会话正在被垃圾收集,您就不会中断垃圾收集器:

public class SessionInfo
{
    private readonly WeakReference _session;

    public SessionInfo(ISession session)
    {
        _session = new WeakReference(session);
    }

    public ISession Session
    {
        get { return (ISession)_session.Target; }
    }
}

创建一个用于存储打开的会话的列表:

List<SessionInfo> OpenSessions = new List<SessionInfo>();

在 DAL(数据访问层)中,我有这种方法:

public ISession GetNewSession()
{
    if (_sessionFactory == null)
        _sessionFactory = createSessionFactory();      
    ISession session = _sessionFactory.OpenSession();
    OpenSessions.Add(new SessionInfo(session));
    return session;
}

这样我可以维护一个打开的会话列表需要的时候可以查询。也许这满足您的需求?

As far as I know, there is no support for getting a list of open sessions from the session factory. I have my own method to keep an eye on open sessions and I use this construction:

Create a class with a weak reference to a ISession. This way you won't interupt the garbage collector if sessions are being garbage collected:

public class SessionInfo
{
    private readonly WeakReference _session;

    public SessionInfo(ISession session)
    {
        _session = new WeakReference(session);
    }

    public ISession Session
    {
        get { return (ISession)_session.Target; }
    }
}

create a list for storing your open sessions:

List<SessionInfo> OpenSessions = new List<SessionInfo>();

and in the DAL (data access layer) I have this method:

public ISession GetNewSession()
{
    if (_sessionFactory == null)
        _sessionFactory = createSessionFactory();      
    ISession session = _sessionFactory.OpenSession();
    OpenSessions.Add(new SessionInfo(session));
    return session;
}

This way I maintain a list of open sessions I can query when needed. Perhaps this meets your needs?

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