nhibernate会话管理器的实现

发布于 2025-01-04 02:17:51 字数 2001 浏览 2 评论 0原文

我是 Nhibernate 的新手,并且在学习过程中放慢了速度。我尝试实现一个会话管理器类来帮助我获取数据库调用的会话。下面是它的代码。有人可以说这在架构上是否正确并预见到可扩展性或性能的任何问题吗?

public static class StaticSessionManager
{
    private static ISession _session;

    public static ISession GetCurrentSession()
    {
        if (_session == null)
            OpenSession();

        return _session;
    }

    private static void OpenSession()
    {
        _session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
    }
    public static void CloseSession()
    {
        if (_session != null)
        {
            _session.Close();
            _session = null;
        }
    }
}

在我的数据提供程序类中,我使用以下代码来获取数据。

    public class GenericDataProvider<T>
    {
            NHibernate.ISession _session;

            public GenericDataProvider()
            {
                this._session = StaticSessionManager.GetCurrentSession();
            }

            public T GetById(object id)
            {
                using (ITransaction tx = _session.BeginTransaction())
                {
                    try
                    {
                        T obj = _session.Get<T>(id);
                        tx.Commit();
                        return obj;
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                        StaticSessionManager.CloseSession();
                        throw ex;
                    }
                }
            }
    }

然后

public class UserDataProvider : GenericDataProvider<User>
{
    public User GetUserById(Guid uid)
    {
        return GetById(uid)

    }
}

页面中的最终用法

UserDataProvider  udp = new UserDataProvider();
User u = udp.GetUserById(xxxxxx-xxx-xxx);

这是正确的吗?在单个页面中实例化大量数据提供程序会导致问题吗?

我现在还面临一个问题,如果我同时从多台机器执行相同的读取操作,Nhibernate 会抛出随机错误 - 我认为这是由于事务造成的。

请指教。

I am new to Nhibernate and slowing working my way thru learning it. I tried to implement a session manager class to help me get the session for my db calls. Below is the code for it. Can someone please say if this is architecturally correct and foresee any issue of scalability or performance?

public static class StaticSessionManager
{
    private static ISession _session;

    public static ISession GetCurrentSession()
    {
        if (_session == null)
            OpenSession();

        return _session;
    }

    private static void OpenSession()
    {
        _session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
    }
    public static void CloseSession()
    {
        if (_session != null)
        {
            _session.Close();
            _session = null;
        }
    }
}

and in my data provider class, I use the following code to get data.

    public class GenericDataProvider<T>
    {
            NHibernate.ISession _session;

            public GenericDataProvider()
            {
                this._session = StaticSessionManager.GetCurrentSession();
            }

            public T GetById(object id)
            {
                using (ITransaction tx = _session.BeginTransaction())
                {
                    try
                    {
                        T obj = _session.Get<T>(id);
                        tx.Commit();
                        return obj;
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                        StaticSessionManager.CloseSession();
                        throw ex;
                    }
                }
            }
    }

and then

public class UserDataProvider : GenericDataProvider<User>
{
    public User GetUserById(Guid uid)
    {
        return GetById(uid)

    }
}

Final usage in Page

UserDataProvider  udp = new UserDataProvider();
User u = udp.GetUserById(xxxxxx-xxx-xxx);

Is this something that is correct? Will instantiating lot of data providers in a single page cause issues?

I am also facing an issue right now, where if I do a same read operation from multiple machines at the same time, Nhibernate throws random errors- which I think is due to transactions.

Please advice.

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

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

发布评论

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

评论(1

无敌元气妹 2025-01-11 02:17:51

据我所知,如果您有一个 null 会话,那么您正在构建会话工厂。您应该只在应用程序启动时调用 BuildSessionFactory() 一次。

在哪里执行此操作取决于您,有些人在 Global.asax 中的方法 application_start 中构建 SessionFactory ,或者在您的情况下有一个 sessionFactory< 的静态属性/code> 而不是 StaticSessionManager 类中的 session

我怀疑您的错误是由于您的会话工厂被多次构建所致!

还有一点是,有些人在每个请求开始时打开一个事务_session.BeginTransaction(),并在请求结束时commitrollback每个请求。这为您提供了一个工作单元,这意味着您可能会丢失

using (ITransaction tx = _session.BeginTransaction())  
{
 ...
}

每个方法。所有这些都有待讨论,但我 99% 的代码都使用这种方法,没有任何问题。

From what I can see you are building the session factory if you have a null session. You should only call BuildSessionFactory() once when the application starts.

Where you do this is up to you, some people build the SessionFactory inside Global.asax in the method application_start or in your case have a static property for sessionFactory instead of session in your StaticSessionManager class.

I suspect your errors are due to the fact that your session factory is being built multiple times!

Another point is that some people open a transaction _session.BeginTransaction() at the beginning of each request and either commit or rollback at the end of each request. This gives you a unit of work which means you can lose the

using (ITransaction tx = _session.BeginTransaction())  
{
 ...
}

on every method. All of this is open for debate but I use this method for 99% of all my code with no trouble at all.

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