这个问题在我的 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 在该框架内被调用,并且我在重建它时遇到了麻烦。
是否有一种方法可以在开始事务时无需显式设置即可实现此目的?
最佳答案
您确定是这样吗?如果您只是根据 NProf 告诉您的内容来验证隔离级别;这可能是个问题。请记住,NHProf 仅报告 NHibernate 中的日志记录基础设施为其提供的内容。当您使用默认值打开事务时,也许不会发送隔离级别消息?这可能可以通过调查 NHibernate 源代码来验证。
我建议在得出这没有按预期工作的结论之前使用替代方法来验证事务级别(也许是 SQL Profiler?)。
编辑:
我已经查看了 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 来说不再是问题了。
关于sql-server - 无法设置默认的 Nhibernate 隔离级别(例如通过映射),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697592/