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

时间:2010-09-13 02:41:54

标签: sql-server nhibernate fluent-nhibernate sharp-architecture

这对我来说是一个在3个项目中存在的问题。

我尝试了以下内容:

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

在hibernate.cfg.xml中设置

使用流利的nhiberate:

MsSqlConfiguration.MsSql2008.IsolationLevel(IsolationLevel.ReadCommitted);

在global.asax.cs中设置

我一直被迫这样设置:

CurrentNhibernateSession.Transaction.Begin(IsolationLevel.ReadCommitted);

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

问题是现在我使用的是尖锐的架构,并且在该框架内调用了transaction.begin,我无法重建它。

有没有一种方法可以在开始交易时没有明确设置它?

2 个答案:

答案 0 :(得分: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...

}

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

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

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

<强>更新

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

答案 1 :(得分:0)

看起来您的属性设置应该被称为hibernate.connection.isolation ...

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

此致

乔恩