I am in a project which uses below framework
- ASP.NET MVC4
- NHibernate as an ORM
- NInject as an IoC to manage dependencies and do DI
The issue is that after user updated an entity and submitted the form, he must wait about 20 seconds to have correct data loaded. Also this time the changes are saved into database (SQL Server)
We have manage NHibernate ISession object in RequestScope but found out that the Deactivation method is not calling when MVC redirect from edit to list screen.
Below is main part of the NInject module for NHibernate:
Load method:
Bind<ISession>()
.ToMethod(c => c.Kernel.Get<ISessionFactory>().OpenSession(interceptor))
.InRequestScope()
.OnActivation(SetupSession())
.OnDeactivation(CommitOrRollback)
private Action<ISession> SetupSession()
{
_logger.Debug("OnActivation");
return s =>
{
s.BeginTransaction(IsolationLevel.ReadCommitted);
s.FlushMode = FlushMode.Commit;
};
}
private void CommitOrRollback(ISession session)
{
_logger.Debug("OnDeactivation");
if (!session.Transaction.IsActive)
{
return;
}
try
{
session.Transaction.Commit();
session.Flush();
_logger.Debug("Session committed successfully");
}
catch (Exception ex)
{
session.Transaction.Rollback();
throw new CommitDataException(ex);
}
}
Thanks for any advice !