Nhibernate в asp,net ISession помогите
Мы используем nhibernate in и asp.net MVC приложение.
Мы реализуем шаблон Session per Request через 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-
2 ответа
Вы должны позвонить утилизировать и не отключать или закрывать. Реализующие объекты System.IDisposable всегда должны быть удалены путем вызова метода dispose или с помощью блока using.
Вы также можете посмотреть код в блоге Бена Дея об управлении сессиями.