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

Ali*_*air 6 sql-server nhibernate fluent-nhibernate sharp-architecture

这对我来说已经存在于3个项目中的问题.

我尝试过以下方法:

<property name="connection.isolation">ReadCommitted</property>
Run Code Online (Sandbox Code Playgroud)

在hibernate.cfg.xml中设置

使用流利的nhiberate:

MsSqlConfiguration.MsSql2008.IsolationLevel(IsolationLevel.ReadCommitted);
Run Code Online (Sandbox Code Playgroud)

在global.asax.cs中设置

我总是被迫这样设置:

CurrentNhibernateSession.Transaction.Begin(IsolationLevel.ReadCommitted);
Run Code Online (Sandbox Code Playgroud)

哪个有效.(我可以使用NHibernate Profiler看到这个)

问题是现在我使用的是尖锐的架构,并且在该框架内调用了transaction.begin,我在重建它时遇到了麻烦.

有没有办法做到这一点,无需在开始交易时明确设置它?

Dan*_*anP 7

你确定是这样的吗?如果您只是通过NHProf告诉您的隔离级别来验证隔离级别; 这可能是个问题.请记住,NHProf仅报告NHibernate中的日志记录基础结构提供的内容.当您使用默认值打开事务时,可能不会发送隔离级别消息?这可以通过调查NHibernate源来验证.

我建议使用另一种方法来验证事务级别(可能是SQL事件探查器?),然后再认为这不能按预期工作.

编辑:

我已经看过NHibernate的源代码,可以验证我的预感.如果您查看AdoTransaction源,您会注意到它在启动事务时记录隔离级别的IsolationLevel.Unspecified而未指定特定的隔离级别.

// 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...

}
Run Code Online (Sandbox Code Playgroud)

但是,实际事务正在使用配置中指定的隔离级别启动几行,如下所示:

if (isolationLevel == IsolationLevel.Unspecified)
{
isolationLevel = session.Factory.Settings.IsolationLevel;
}
Run Code Online (Sandbox Code Playgroud)

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

更新:

看起来这已经在NHibernate的trunk版本中修复了,所以我猜这不再是NHibernate 3.xx的问题.