我正在使用HttpModule来处理我的NHibernate会话和事务。我有很多页面对于在我的IIS6机箱上没有成功启动的事务非常胡思乱想,但是当我在本地运行它时却没有,特别是当我尝试在PostBack期间调用事务上的Commit()来刷新具有新更新数据的数据网格时。当我在本地调试代码时,不会发生这些错误。
本地和服务器的web.config文件是相同的。
服务器上是否有某些配置可能导致我本地VS Debug Server处理的IIS上的模块出现行为差异?
模块代码如下所示:
public class NHibernateSessionModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += BeginTransaction;
context.EndRequest += CommitAndCloseSession;
}
private static void BeginTransaction(object sender, EventArgs e)
{
if (HttpContext.Current.Request.RawUrl.Contains(".aspx"))
{
NHibernateSessionManager.Instance.InitSessionFactory();
NHibernateSessionManager.Instance.BeginTransaction();
}
}
private static void CommitAndCloseSession(object sender, EventArgs e)
{
if (HttpContext.Current.Request.RawUrl.Contains(".aspx"))
{
try
{
NHibernateSessionManager.Instance.FlushSession();
NHibernateSessionManager.Instance.CommitTransaction();
}
finally
{
NHibernateSessionManager.Instance.CloseSession();
}
}
}
public void Dispose() { }
}
相关经理代码如下:
private void InitSessionFactory()
{
if (sessionFactory != null)
{
return;
}
var cfg = new Configuration();
// The following makes sure the the web.config contains a declaration for the HBM_ASSEMBLY appSetting
if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["HBM_ASSEMBLY"]))
{
throw new ConfigurationErrorsException(
"NHibernateManager.InitSessionFactory: \"HBM_ASSEMBLY\" must be " +
"provided as an appSetting within your config file. \"HBM_ASSEMBLY\" informs NHibernate which assembly " +
"contains the HBM files. It is assumed that the HBM files are embedded resources. An example config " +
"declaration is <add key=\"HBM_ASSEMBLY\" value=\"MyProject.Core\" />");
}
// Don't make session factories for foundations that share a database
string hibernateString = BaseAccess.GetHibernateConnectionString();
cfg.AddAssembly(ConfigurationManager.AppSettings["HBM_ASSEMBLY"]);
cfg.SetProperty("connection.connection_string", hibernateString);
sessionFactory = cfg.BuildSessionFactory();
}
public void BeginTransaction()
{
if (threadTransaction == null ||
threadTransaction.WasCommitted ||
threadTransaction.WasRolledBack ||
!threadTransaction.IsActive)
{
threadTransaction = GetSession().BeginTransaction();
}
}
public ISession GetSession()
{
if (null == sessionFactory)
{
InitSessionFactory();
}
if ((threadSession == null || !threadSession.IsOpen) && null != sessionFactory)
{
threadSession = sessionFactory.OpenSession();
}
return threadSession;
}
private void FlushSession()
{
if (null != threadSession && threadSession.IsOpen)
{
threadSession.Flush();
}
}
public void CommitTransaction()
{
try
{
if (threadTransaction!= null && !threadTransaction.WasCommitted && !threadTransaction.WasRolledBack && threadTransaction.IsActive)
{
threadTransaction.Commit();
threadTransaction = null
}
}
catch (HibernateException)
{
RollbackTransaction();
throw;
}
}
public void RollbackTransaction()
{
try
{
if (threadTransaction != null && !threadTransaction.WasCommitted && !threadTransaction.WasRolledBack && threadTransaction.IsActive)
{
threadTransaction.Rollback();
}
}
finally
{
CloseSession();
}
}
private void CloseSession()
{
if (threadSession != null && threadSession.IsOpen)
{
threadSession.Close();
}
}
答案 0 :(得分:2)
猜测,因为不包括完整的代码: 您将会话存储在线程中并且线程行为不同?您应该将会话存储在httpcontext中,它可能会没问题。