Hibernate OpenSession()与GetCurrentSession()

沙巴兹·沙菲克(Shahbaz Shafique)

我是N休眠的新手。我在应用程序中使用n休眠。我编写的代码可以成功运行,但是运行起来有点慢,因为当我在休眠状态探查器中签入时,它向我显示了流程缓慢的一些原因。“每个请求超过一个会话”我的代码是

          using (ISession session = NContext._mSessionFactory.OpenSession())
          {
              ICriteria criteriaAspNetUser = session.CreateCriteria(typeof(AspNetUsers));
              criteriaAspNetUser.Add(NHibernate.Criterion.Restrictions.Eq("Email", email));
              criteriaAspNetUser.Add(NHibernate.Criterion.Restrictions.Eq("PasswordHash", password)); 

当我使用GetCurrentSession()函数时,它会返回一些异常

没有配置CurrentSessionContext(设置属性current_session_context_class)!

但是当我在配置文件中添加以下代码时

“属性名称=“ current_session_context_class”>线程静态/属性>”

它显示不同的异常是

没有会话绑定到当前上下文

我正在进行最后3天的工作,但找不到任何解决方案,请帮助我,我对此感到担心。

苏哈斯

CurrentSessionContext仅告诉NHibernate应该使用哪个会话上下文实现。根据要编写的应用程序类型,有多个可用选项。配置完成后,您需要将创建的每个会话对象绑定到会话上下文。以下代码说明了如何执行此操作

public class SessionManager
{
    private static readonly ISessionFactory sessionFactory;

    static SessionManager()
    {
        sessionFactory = new DatabaseConfiguration().BuildSessionFactory();
    }

    public static ISession GetCurrentSession()
    {
        if (CurrentSessionContext.HasBind(sessionFactory))
        {
            return sessionFactory.GetCurrentSession();
        }

        var session = sessionFactory.OpenSession();
        CurrentSessionContext.Bind(session);
        return session;
    }

    public static void Unbind()
    {
        CurrentSessionContext.Unbind(sessionFactory);
    }
}

现在,只要需要会话对象,就可以调用SessionManager.GetCurrentSession并且它将在内部执行以下操作

  1. 检查是否有绑定到上下文的现有会话对象,如果是,请从会话工厂返回当前会话,因为那是绑定到上下文的对象
  2. 如果不是,则打开一个新会话,将其绑定到CurrentSessionContext并返回会话对象

请注意,有一种Unbind方法SessionManager可用于解除绑定会话对象。

现在,关于警告more than one session per request该警告明确告诉您,每个请求正在使用多个会话(如果在Web应用程序的上下文中使用它)。应对此警告的一种流行方法是使用“每个请求会话”模式。在这种模式下,对于每个传入请求,您都将在请求命中您的代码时创建一个新的会话对象,在整个请求处理过程中使用该会话对象,并在请求离开您的代码时处置该会话对象。可以将以下代码添加到global.asax.csASP.NET MVC应用程序的文件中,以获取“每个请求的会话”

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var session = SessionManager.GetCurrentSession();
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        var session = SessionManager.GetCurrentSession();
        session.Close();
        session.Dispose();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Hibernate的Sessionfactory.getCurrentSession()和SessionFactory.openSession()的区别

休眠openSession()vs getCurrentSession()

何时使用OpenSession()和GetCurrentSession()

休眠:sessionFactory.openSession()VS sessionFactory.getCurrentSession()

在getCurrentSession()上使用openSession()-何时以及为什么?

Hibernate 4中的SessionFactory.openSession(Connection)

正确配置以模拟Hibernate的sessionFactory.getCurrentSession()

NoSuchMethodError:org.hibernate.SessionFactory.getCurrentSession()

Spring + Hibernate:sessionFactory.getCurrentSession()导致NullPointerException

在Hibernate中使用SessionFactory.getCurrentSession()时获取Connection对象

Hibernate在Transaction内使用openConnection,在Transaction外使用getCurrentSession

java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg / hibernate / classic / Session

hibernate SessionFactory.openSession()是否等待池中的数据库连接可用

调用openSession且无法建立与DB的连接时,Hibernate不会引发异常

在hibernate5.1和spring 4.1.6集成中找不到Hibernatetemplate的opensession()异常

休眠,sessionFactory.openSession挂起

Hibernate Native与Hibernate JPA

getCurrentSession()-NullPointerException-为什么?

Hibernate与EclipseLink

EJB与Hibernate

Hibernate教程

具有WebExtension的OpenSession(pkcs11)

java.lang.NullPointerException session.getCurrentSession()

未为类型SessionFactory定义方法getCurrentSession()

Java Hibernate org.hibernate.annotations.Index

Hibernate找不到hibernate.cfg.xml

Hibernate和Hibernate JPA之间的区别

Hibernate @ManyToMany org.hibernate.LazyInitializationException

Hibernate:如何在Hibernate中删除多行?