简体   繁体   English

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

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

I am using an HttpModule for handling my NHibernate Sessions and Transactions. 我正在使用HttpModule处理我的NHibernate会话和事务。 I have many pages that are being very cranky about transactions not successfully starting on my IIS6 box but not when I run them locally, specifically when I try to call Commit() on the transactions during a PostBack to refresh datagrids with new updated data. 我有很多页面对于事务未在IIS6框上成功启动但在本地运行时未成功启动非常奇怪,特别是当我尝试在PostBack期间在事务上调用Commit()来刷新具有新的更新数据的数据网格时。 These errors do NOT occur when I am debugging the code locally. 当我在本地调试代码时,不会发生这些错误。

The web.config files for the local and the server are the same. 本地服务器和服务器的web.config文件相同。

Is there some configuration on the server that could be causing behavioral differences with the module on IIS that my local VS Debug Server handles happily? 服务器上是否有某些配置可能导致与本地VS Debug Server可以轻松处理的IIS模块上的行为发生差异?

The module code looks like this: 模块代码如下所示:

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() { }
}

The relevant manager code is as follows: 相关的经理代码如下:

    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();
        }
    }

A guess because the complete code is not included: You store the session in the thread and the threading behaves different? 一个猜测,因为其中不包括完整的代码:您将会话存储在线程中,并且线程的行为有所不同吗? You should store the session in the httpcontext and it will probably be fine. 您应该将会话存储在httpcontext中,可能会很好。

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

相关问题 WCF(de)序列化在debug / Visual Studio和IIS下的行为不同 - WCF (de) serialization behaving differently under debug/Visual Studio and IIS Visual Studio 2008不会调试 - Visual Studio 2008 would not debug 是否可以调试Visual Studio以查看SqlConnection事务 - Is it possible to debug visual studio to view SqlConnection transactions UnitOfWork Sessions中的NHibernate事务 - NHibernate transactions within UnitOfWork Sessions 多个会话在 oracle db 中由 do.net web 应用程序在实时服务器的 visual studio 2008 中创建 - Multiple sessions are created in oracle db by dotnet web application in visual studio 2008 in live server 如何在Visual Studio 2008中调试* Invoke() - How to debug *Invoke() in Visual Studio 2008 在调试模式下在Visual Studio 2008中编辑代码 - Editing code in Visual Studio 2008 in debug mode Visual Studio 2008调试不会因错误而停止 - Visual Studio 2008 debug does not stop on error 有没有办法使Nhibernate 3与使用Visual Studio 2008的Sharp Architecture一起使用? - Is there a way to get Nhibernate 3 to work with Sharp Architecture using Visual Studio 2008? 是否可以在Visual Studio调试会话之间保留cookie - Is it possible to persist cookies between visual studio debug sessions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM