簡體   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