繁体   English   中英

NHibernate会话/事务在IIS6和Visual Studio 2008调试服务器上的行为不同

[英]NHibernate sessions/transactions acting differently on IIS6 and Visual Studio 2008 Debug Server

我正在使用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();
        }
    }

一个猜测,因为其中不包括完整的代码:您将会话存储在线程中,并且线程的行为有所不同吗? 您应该将会话存储在httpcontext中,可能会很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM