“在多个线程中使用单个会话可能是一个错误”使用 NServiceBus 时 NProf 出现错误

发布于 2024-08-27 21:36:34 字数 801 浏览 6 评论 0原文

当执行使用 NHibernate 进行数据访问操作的 NServiceBus 处理程序时,我看到一个错误,我不确定是否需要关注该错误。

处理程序的代码执行类似以下操作:

using (var tx = Session.BeginTransaction()) 
{
    var accountGroup = _groupRepository.FindByID(message.GroupID);
    accountGroup.CreateAccount(message.AccountNumber);

    tx.Commit();
}

当我分析此进程时,我看到以下行:

  • 分布式事务中的登记会话,隔离级别:可序列化
  • 开始事务,隔离级别:未指定
  • SELECT ... FROM AccountGroups this_ WHERE this_.ID = 123
  • INSERT INTO Accounts ...
  • commit transaction
  • 提交事务

第一条提交消息是在我调用 tx.Commit() 时由我的代码生成的。我相信第二条提交消息是在我们离开处理程序的 Handle 方法并由 NServiceBus 调用时发生的。第二次调用 commit 会在 NProf 中生成一条警报,指出“在多个线程中使用单个会话可能是一个错误”。

我不认为这是一个问题,因为当时确实没有什么可承诺的,但是我在这里做了一些不合适的事情吗?我确实想在事务中运行我的代码,但是当我这样做时,我会收到此警报。

有什么想法吗?

When executing an NServiceBus handler that uses NHibernate for its data access operations, I am seeing an error that I am not sure if I need to be concerned with.

The handler has code that does something like this:

using (var tx = Session.BeginTransaction()) 
{
    var accountGroup = _groupRepository.FindByID(message.GroupID);
    accountGroup.CreateAccount(message.AccountNumber);

    tx.Commit();
}

When I profile this process, I see the following lines:

  • enlisted session in distributed transaction with isolation level: Serializable
  • begin transaction with isolation level: Unspecified
  • SELECT ... FROM AccountGroups this_ WHERE this_.ID = 123
  • INSERT INTO Accounts ...
  • commit transaction
  • commit transaction

The first commit message is generated by my code when I call tx.Commit(). The second commit message, I believe occurs when we leave the Handle method of the handler and is called by NServiceBus. This second call to commit generates an alert in NHProf that states "Using a single session in multiple threads is likely a bug".

I don't think this is an issue, because there really is nothing to commit at that time, but am I doing some inappropriate here? I do want to run my code within a transaction, but when I do, I get this alert.

Any ideas?

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

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

发布评论

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

评论(1

牵强ㄟ 2024-09-03 21:36:34

这不是问题,发生的情况是 NH Prof 检测到 DTC 提交发生在另一个线程中。
它实际上应该正确处理 DTC 提交,所以我不确定发生了什么。据猜测,同时使用 DTC 提交和标准提交会让人感到困惑。
我会解决它。

This isn't an issue, what is happening is that NH Prof detects that the DTC commit is happening in another thread.
It should actually handle DTC commits properly, so I am not sure what is going on. At a guess, using both DTC commit and standard commit it confusing it.
I'll fix it.

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