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

发布于 2024-09-19 01:50:22 字数 606 浏览 7 评论 0原文

这是我的 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 在该框架内被调用,并且我在重建它时遇到了麻烦。

有没有一种方法可以在开始交易时无需显式设置即可实现此目的?

This has been a problem that has existed on 3 projects for me.

I have tried the following:

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

Set in hibernate.cfg.xml

Using fluent nhiberate:

MsSqlConfiguration.MsSql2008.IsolationLevel(IsolationLevel.ReadCommitted);

Set in global.asax.cs

I have always been forced to set it like so:

CurrentNhibernateSession.Transaction.Begin(IsolationLevel.ReadCommitted);

which works. (I can see this using NHibernate Profiler)

The problem is now I am using sharp architecture and transaction.begin is called inside that framework and I am having trouble rebuilding it.

Is there a way to do this that works without explicitly setting it when you begin a transaction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Bonjour°[大白 2024-09-26 01:50:22

您确定是这样吗?如果您只是根据 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 来说不再是问题了。

Are you certain that this is the case? If you're only verifying the isolation level by what NHProf is telling you; that could be a problem. Remember that NHProf is only reporting what the logging infrastructure in NHibernate feeds it. Perhaps the isolation level message isn't sent when you open a transaction with the default? This could probably be verified by investigating the NHibernate source.

I would suggest using an alternate means to verify the transaction level (perhaps SQL Profiler?) before concluding that this isn't working as expected.

Edit:

I've had a look at the NHibernate source and can verify my hunch above. If you have a look at the AdoTransaction source you'll note that it is logging an isolation level of IsolationLevel.Unspecified when a transaction is started without specifying a specific isolation level.

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

}

However, the actual transaction is being started with the isolation level specified in the config a few lines down as such:

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

So, it would seem that NHProf is not being entirely accurate in this instance.

Update:

It appears this has been fixed in the trunk version of NHibernate, so I'm guessing this is no longer an issue with NHibernate 3.xx.

柠檬色的秋千 2024-09-26 01:50:22

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

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

问候

乔恩

Looks like your property setting should be called hibernate.connection.isolation...

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

Regards

Jon

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文