代码之家  ›  专栏  ›  技术社区  ›  Alistair

无法设置默认Nhibernate隔离级别(例如通过映射)

  •  6
  • Alistair  · 技术社区  · 15 年前

    这是一个问题,已经存在于3个项目为我。

    我尝试了以下方法:

    <property name="connection.isolation">ReadCommitted</property>
    

    开始休眠.cfg.xml

    使用fluent nhiberate:

    MsSqlConfiguration.MsSql2008.IsolationLevel(IsolationLevel.ReadCommitted);
    

    我总是被迫这样设置:

    CurrentNhibernateSession.Transaction.Begin(IsolationLevel.ReadCommitted);
    

    这很有效。(我可以使用NHibernate Profiler看到这一点)

    有没有一种方法可以在您开始事务时不显式地设置它而工作?

    2 回复  |  直到 15 年前
        1
  •  7
  •   DanP    15 年前

    你确定是这样吗?如果你只是通过NHProf告诉你的来验证隔离级别,那可能是个问题。请记住,NHProf只报告NHibernate中的日志基础设施为其提供了什么。当您使用默认值打开事务时,可能不会发送隔离级别消息?这可能可以通过调查NHibernate来源来证实。

    我建议使用另一种方法来验证事务级别(也许是SQL Profiler?)在得出结论之前,这不是预期的工作。

    编辑:

    AdoTransaction source您将注意到它记录的隔离级别为隔离级别。未指定事务启动时没有指定特定的隔离级别。

    // This is the default overload you're calling:
    public void Begin()
    {
    Begin(IsolationLevel.Unspecified);
    }
    
    // This is the full method impl
    public void Begin(IsolationLevel isolationLevel)
    {
    // snip....
    
    // This is the problematic line here; it will report an isolationLevel of Unspecified.
    log.Debug(string.Format("Begin ({0})", isolationLevel));
    
    // snip...
    
    }
    

    但是,实际事务是以配置文件中指定的隔离级别启动的,如下所示:

    if (isolationLevel == IsolationLevel.Unspecified)
    {
    isolationLevel = session.Factory.Settings.IsolationLevel;
    }
    

    因此,在这种情况下,NHProf似乎并不完全准确。

    看来这已经在NHibernate的主干版本中修复了,所以我猜这不再是nhibernate3.xx的问题了。

        2
  •  0
  •   Jon    15 年前

    <add key="hibernate.connection.isolation" value="ReadCommitted" />
    

    当做