NServiceBus 和 NHibernate - 消息处理程序和事务

发布于 2024-08-08 06:07:15 字数 835 浏览 2 评论 0原文

根据我的理解,NServiceBus 在事务内执行 IMessageHandler 的 Handle 方法,如果异常从该方法传播出去,那么 NServiceBus 将确保将消息放回消息队列(在错误队列之前最多 X 次)等。所以可以说我们有一个原子操作。

现在,如果我在 NServiceBus Message Handle 方法中执行类似的操作,

using(var trans = session.BeginTransaction())
{ 

  person.Age = 10;
  session.Update<Person>(person);
  trans.Commit()
}

using(var trans2 = session.BeginTransaction())
{ 

  person.Age = 20;
  session.Update<Person>(person);
  // throw new ApplicationException("Oh no");
  trans2.Commit()
}

这对事务范围有何影响? 尽管我们没有采取任何措施将它们结合起来,但就其与 Nservicebus 事务的关系而言,trans1 现在是否算作嵌套事务? (如果不是,如何链接到 NServiceBus 的事务?

看看第二个块(trans2),如果我取消注释 throw 语句,NServiceBus 事务也会回滚 trans1 吗?在基本场景中,假设我将上述内容转储到控制台应用程序,然后 trans1 是独立的,提交,刷新并且不会回滚我试图澄清现在我们坐在其他人的事务中(例如 NServiceBus)会发生什么?

上面只是示例代码,我不会直接使用会话,更像是通过 uow 模式。

From my understanding NServiceBus executes the Handle method of an IMessageHandler within a transaction, if an exception propagates out of this method, then NServiceBus will ensure the message is put back on the message queue (up X amount of times before error queue) etc.. so we have an atomic operation so to speak.

Now when if I inside my NServiceBus Message Handle method I do something like this

using(var trans = session.BeginTransaction())
{ 

  person.Age = 10;
  session.Update<Person>(person);
  trans.Commit()
}

using(var trans2 = session.BeginTransaction())
{ 

  person.Age = 20;
  session.Update<Person>(person);
  // throw new ApplicationException("Oh no");
  trans2.Commit()
}

What is the effect of this on the transaction scope?
Is trans1 now counted as a nested transaction in terms of its relationship with the Nservicebus transaction even though we have done nothing to marry them up? (if not how would one link onto the transaction of NServiceBus?

Looking at the second block (trans2), if I uncomment the throw statement, will the NServiceBus transaction then rollback trans1 as well? In basic scenarios, say I dump the above into a console app, then trans1 is independent, commit, flushed and won't rollback. I'm trying to clarify what happens now we sit in someone else's transaction like NServiceBus?

The above is just example code, im wouldnt be working directly with session, more like through a uow pattern.

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

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

发布评论

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

评论(1

狼性发作 2024-08-15 06:07:15

如果将端点标记为事务(.MsmqTransport().IsTransactional(true) 或仅 AsA_Server),则事务将登记到打开的一个 NServiceBus 中。这意味着您在处理程序中的提交实际上不会发生,整个事情将一起提交或回滚 - 除非您明确告诉您的事务不要加入环境事务。

无论您是直接使用会话还是通过 UoW 工作,您似乎都想对给定消息执行多个操作 - 为什么?该消息已经是自然的 UoW。

If you mark your endpoint as transaction (.MsmqTransport().IsTransactional(true) or just AsA_Server) then the transactions will enlist into the one NServiceBus opened. What this means is that the commits you have inside your handler won't actually happen and the whole thing will either commit or rollback together - unless you specifically tell your transactions not to enlist in the ambient transaction.

Whether or not you work directly with the session or through a UoW, it looks like you want to do more than one for a given message - why? The message is already the natural UoW.

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