实现 NHibernate 嵌套事务行为 [英] Achieving NHibernate Nested Transactions Behavior

查看:25
本文介绍了实现 NHibernate 嵌套事务行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NHibernate 的事务控制和 FlushMode 选项来实现某种嵌套的事务行为,但是在阅读过多之后事情变得有点混乱,因此对我在下面列出的事实的任何确认都将非常有用.

I'm trying to achieve some kind of nested transaction behavior using NHibernate's transaction control and FlushMode options, but things got a little bit confusing after too much reading, so any confirmation about the facts I list below will be very usefull.

我想要的是打开一个分成小交易的大交易.想象一下以下场景:

What I want is to open one big transaction that splits in little transactions. Imagine the following scenario:

  • TX1 打开一个 TX 并插入一个 Person 的记录;
  • TX2 打开一个 TX 并将此人的名字更新为 P2;
  • TX2 提交;
  • TX3 打开一个 TX 并将此人的名字更新为 P3;
  • TX3 回滚;
  • TX1 提交;

我希望看到 NH 将 INSERT 和 TX2 UPDATE 发送到数据库,只是忽略了 TX3,因为它被回滚了.

I'd like to see NH sending the INSERT and the TX2 UPDATE to the database, just ignoring what TX3, as it was rolled back.

我尝试使用 FlushMode = Never 并且仅在要求正确的开始/提交/回滚后刷新会话,但 NH 始终使用对象的最终状态更新数据库,与提交和回滚无关.这正常吗?NH 在使用 FlushMode = Never 时真的会忽略事务控制吗?

I tried to use FlushMode = Never and only flushing the session after the proper Begins/Commits/Rollbacks have been demanded, but NH always update the database with the object's final state, independent of commits and rollbacks. Is that normal? Does NH really ignores transactional control when working with FlushMode = Never?

我还尝试使用 FlushMode = Commit 并打开嵌套事务,但我发现,因为 ADO.NET,嵌套事务实际上始终是同一个事务.

I've also tried to use FlushMode = Commit and openning the nested transactions, but I discovered that, because ADO.NET, the nested transactions are, actually, always the same transaction.

请注意,我并不是要实现全有或全无"的行为.我正在寻找更多的保存点工作方式.NH 有没有办法做到这一点(保存点)?

Note that I'm not trying to achieve a "all or nothing" behavior. I'm looking more to a savepoint way of working. Is there a way to do that (savepoints) with NH?

提前致谢.

菲利普

推荐答案

为了不要让这个问题永远悬而未决,我将发布我们采用的解决方案.

Just to don't leave this question open forever, I'll post the solution that we've adopted.

我们有一个类似容器的工作单元来管理嵌套的事务行为.根据我们想要的治疗类型,它会创建(或不创建)新会话.举例:

We have a unit of work like container that manages the nested transaction behavior. Depending on the kind of treatment that we want, it creates (or not) new sessions. By example:

  • 继续出错:如果我们希望即使发生其他事务错误,UoW 容器也会为每个事务"使用不同的会话,并在其工作结束时刷新每个 tx;
  • 错误回滚:如果我们希望在会话回滚(由于错误或业务回滚)时每隔一个事务回滚,UoW 容器对所有嵌套事务使用相同的会话,并回滚其中的每个人结束.

重要的是要说这个 UoW 操纵的事务"不是直接的 NH (ADO.NET) 事务.我们已经创建了一个事务的抽象,因此操作代码会投票"我们的事务是否可能被提交或回滚,但实际操作只会发生在所有事情的最后,基于所选的错误策略.

It's important to say that the "transaction" that this UoW manipulates isn't the NH (ADO.NET) transaction directly. We've created an abstraction of a transaction so the manipulation code "votes" if our transaction might be commited or rolled back, but the real action just occurs in the end of everything, based on the selected error strategy.

我们知道这种用法不是很常见,只适用于特定场景(在我们的例子中它是带有批处理的集成场景),所以我现在将发布代码.如果有人认为此实现可以提供帮助,请给我发消息,我很乐意分享代码.

We know that this usage isn't very common and only fits on specific scenarios (in our case it's an integration scenario with batch processing), so I'll now post code. If anyone thinks that this implementation can help, please, send me a message and I'll be glad to share the code.

问候,

菲利普

这篇关于实现 NHibernate 嵌套事务行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆