dah*_*byk 8 asp.net nhibernate
NHibernate版本:2.1
我正在使用似乎是一个非常标准的HttpModule方法来实现ASP.NET + NHibernate应用程序中的每个请求会话.我正试图利用WebSessionContext,但它似乎没有正常工作.具体来说,对于应用程序上的第一个请求,一切都运行良好,但是其他请求会导致"会话关闭!" 任何时候使用会话的异常.重置应用程序池允许另一个请求成功,然后更多"会话关闭!".
有一些活动的部分,但我不知道如何管理上下文以缩小它...所以这就是一切!
在web.config中:
<property name="current_session_context_class">
NHibernate.Context.WebSessionContext, NHibernate
</property>
Run Code Online (Sandbox Code Playgroud)
(我也尝试将它设置为'web',结果相同.)
确认配置正确的模块:
public class NHibernateSessionModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
Debug.WriteLine("NHibernateSessionModule.Init()");
context.BeginRequest += context_BeginRequest;
context.EndRequest += context_EndRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
Debug.WriteLine("NHibernateSessionModule.BeginRequest()");
var session = NHibernateHelper.OpenSession();
session.BeginTransaction();
CurrentSessionContext.Bind(session);
}
void context_EndRequest(object sender, EventArgs e)
{
Debug.WriteLine("NHibernateSessionModule.EndRequest()");
var session = NHibernateHelper.GetCurrentSession();
if (session != null)
{
try
{
if (session.Transaction != null && session.Transaction.IsActive)
session.Transaction.Commit();
}
catch (Exception ex)
{
session.Transaction.Rollback();
throw new ApplicationException("Error committing database transaction", ex);
}
finally
{
session.Close();
}
}
CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的小帮手:
public class NHibernateHelper
{
public static readonly ISessionFactory SessionFactory;
static NHibernateHelper()
{
try
{
Configuration cfg = new Configuration();
cfg.AddAssembly(Assembly.GetCallingAssembly());
SessionFactory = cfg.Configure().BuildSessionFactory();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw new ApplicationException("NHibernate initialization failed", ex);
}
}
public static ISession GetCurrentSession()
{
return SessionFactory.GetCurrentSession();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
Run Code Online (Sandbox Code Playgroud)
NHibernate 1.2 的示例(来自 NHibernate in Action)显示取消绑定是在关闭之前完成的。
这种顺序的改变有帮助吗?
var session = NHibernateHelper.GetCurrentSession();
CurrentSessionContext.Unbind(NHibernateHelper.SessionFactory);
...
session.Close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4433 次 |
| 最近记录: |