如何使用Nhibernate在ASP.NET MVC中实现按请求的会话模式 [英] How to implement session-per-request pattern in asp.net mvc with Nhibernate
问题描述
我在global.asax文件的Application_start事件中创建了nhibernate会话,该会话正在传递给服务方法的构造函数.
在服务方法中,我使用会话执行CRUD操作,这很好.但是,当发生多个请求或并行事务时,nhibernate抛出了一些异常.在阅读了论坛之后,我知道Nhibernate会话不是线程安全的.如何使其具有线程安全性,并让我的应用程序(ASP.NET mvc)与并行事务一起工作?
使线程安全的唯一方法是为每个请求创建一个新会话,您可以在NHibernate配置中对managed_web
使用current_session_context_class
属性. /p>
在global.asax中
protected void Application_BeginRequest(object sender, EventArgs e)
{
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
}
protected void Application_EndRequest(object sender, EventArgs e)
{
var session = CurrentSessionContext.Unbind(SessionFactory);
//commit transaction and close the session
}
现在,当您想访问会话时,可以使用
Global.SessionFactory.GetCurrentSession()
如果您使用的是DI容器,则通常是内置在该容器中的,
以Autofac为例(请参阅此问题以获取更多信息)
containerBuilder.Register(x => {
return x.Resolve<ISessionFactory>().OpenSession();
}).As<ISession>().InstancePerHttpRequest();
I created the nhibernate session in Application_start event of global.asax file,the session is being passed to constructors of service methods.
In the service method I am using the session to do CRUD operations, this works fine.However, When multiple requests or parallel transactions occuring nhibernate is throwing some exceptions.After reading forums i came to know that Nhibernate session is not thread safe.How to make it thread safe and let my application (ASP.NET mvc) work with parallel trandsactions?
Only way to make it thread safe is to create a new session per each request, you could use current_session_context_class
property to managed_web
in NHibernate config.
In global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
}
protected void Application_EndRequest(object sender, EventArgs e)
{
var session = CurrentSessionContext.Unbind(SessionFactory);
//commit transaction and close the session
}
now when you want to access the session, you could use,
Global.SessionFactory.GetCurrentSession()
If you are using a DI container, it's usually built into the container,
For example for Autofac (see this question for more information),
containerBuilder.Register(x => {
return x.Resolve<ISessionFactory>().OpenSession();
}).As<ISession>().InstancePerHttpRequest();
这篇关于如何使用Nhibernate在ASP.NET MVC中实现按请求的会话模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!