【发布时间】:2010-12-22 13:22:32
【问题描述】:
我们在和 asp.net MVC 应用程序中使用 nhibernate。
我们正在通过 httpModule 实现每个请求的会话模式。
它看起来很简单,但是当我们使用 NHibernate Profiler 运行时,它清楚地表明 会话永远不会关闭。
模式看起来很简单......但我不明白为什么会话永远不会结束。
这是我认为很重要的代码。
设置事件处理程序:
context.EndRequest += new EventHandler(this.context_EndRequest);
在处理程序中处理会话
private void context_EndRequest(object sender, EventArgs e)
{
netLogHdl.ArchDebug("NHibernateHttpModule.context_EndRequest() ");
Dispose(0);// we are hitting 2 dbs and thus keep one session for each.
Dispose(1);
HttpContextBuildPolicy.DisposeAndClearAll();
}
private void Dispose(int sessionIndex)
{
netLogHdl.ArchStart("NHibernateHttpModule.Dispose", "int sessionIndex=\" + sessionIndex + \")");
try
{
//close the DB session
string sessManagerName = "";
string jcdcManager = "JCDC Manager";
string spamisManager = "Spamis Manager";
if (sessionIndex == 0)
sessManagerName = jcdcManager;
else
{
sessManagerName = spamisManager;
}
ISession oneSession = sessionPerDB[sessionIndex];
if (oneSession != null)
{
if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " oneSession is NOT null");
if (oneSession.IsOpen)
{
// Don't flush - all saves should use transactions and calling Commit does the flush.
if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Closing the session");
//This will overrite it with the exact same session, if they don't match something weird is going on - EWB
oneSession = CurrentSessionContext.Unbind(factoryPerDB[sessionIndex]);
oneSession.Close();
}
else
{
if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session is NOT open");
}
//if ( sessManagerName == jcdcManager ) netLogHdl.ArchDebug( sessManagerName + " Session got Dispose()-ing" );
//oneSession.Dispose();
}
else
{
if (sessManagerName == jcdcManager) netLogHdl.ArchDebug(sessManagerName + " Session is NULL");
}
sessionPerDB[sessionIndex] = null;
}
catch (Exception)
{
throw;
}
netLogHdl.ArchEnd();
}
谁能指出我正确的方向?我应该看什么,模式没有正确实现吗?
我很困惑
谢谢!
E-
【问题讨论】:
-
在上面代码的注释中,它说“不要刷新 - 所有保存都应该使用事务,调用 Commit 进行刷新。”,但代码既不刷新也不使用事务。我认为这是个问题。
-
事务与会话不同,他可能是在 http 模块之外处理它,就像我一样。它是 Session-Per-Request,而不是 Transaction-Per-Request。
-
是的,事务正在此层之外提交。我确实在关闭之前添加了一个刷新,以防会话由于某种原因没有使用事务(即获取)
标签: asp.net nhibernate isession