TransactionScope 和回滚对象状态

发布于 2024-07-26 20:59:29 字数 813 浏览 7 评论 0原文

我正在寻找设计问题的解决方案。 这需要一些解释。 我会发布代码,但这会使它变得更长。

我有一个自定义通用集合,用于根据需要保存业务对象。 为了便于引用,将业务对象称为BO,将通用集合称为GC。 在 GC 内部,我有一个已标记为删除的业务对象的私有集合。 将此私人收藏称为PDC

我可以在任何时候拥有任意数量的 GC(每个 GC 都有自己的 PDC),以及不在任何集合中的其他 BO。

当我保存更改时,我会循环所有 BO 和 GC,并让每个人保存其更改。 这种情况发生在 TransactionScope 中,因此如果有任何内容无法正确保存,我会回滚数据库更改。

当 GC 保存时,我对其 PDC 的状态有疑问。 GC 首先保存所有有更新的 BO,然后删除 PDC 中与 BO 关联的记录,然后清除 PDC 中的所有 BO。 我这样做是为了让 GC 的状态正确反映数据库的新状态。

现在假设一个或多个 GC 成功保存后,BO 或 GC 未能保存。 TransactionScope 执行回滚。 从数据库中删除的记录已恢复,但部分/全部 PDC 已被清除,且状态信息已丢失

所以这是我的困惑:如何在提交发生之前保留 PDC 信息,然后保证清除适当的集合?

TransactionScope 没有我可以捕获的事件来让我知道更改何时已提交。 任何给定事务可能会影响许多 BO 和 GC,因此我无法限制该事务一次处理一个 GC。

有什么建议么?

I'm looking for a solution to a design problem. This will take a bit of explaining. I would post code, but that woul make this even longer.

I have a custom generic collection I use to hold Business Objects as needed. For easy of reference, call the business objects BO and the generic collection GC. Inside GC I have a private collection of those business objects that have been flagged for deletion. Call this private collection PDC.

I can have an arbitrary number of GC's, each with their own PDC, at any one time, plus other BOs that aren't in any collection.

When I save changes I loop over all BO and GC and have each one save their changes. This happens wrapped in a TransactionScope so I rollback database changes if anything fails to save properly.

When a GC saves I have a problem with the state of its PDC. The GC first saves all BOs with updates, then deletes records associated with the BOs in the PDC, then clears the PDC of all BOs. I do this so the state of the GC correctly reflects the new state of the database.

Now suppose a BO or GC fails to save after one or more of the GC has successsfully saved . The TransactionScope performs a rollback. The records deleted from the database are restored, but the some/all of the PDC's have been cleared and that state information lost.

So here is my quandry: How do I keep the PDC information around until after the commit has occured, then guarantee the appriopriate collections are cleared?

TransactionScope has no event I can catch to let me know when it the changes have been committed. There are potentially MANY BOs and GC affected by any given transaction, so I can't restrict the transaction to handle one GC at a time.

Any suggestions?

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

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

发布评论

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

评论(1

勿忘初心 2024-08-02 20:59:30

TransactionStarted 由事务管理器引发,TransactionCompleted 由事务本身引发。

您可能希望 GC 实现 IEnlistmentNotification; 有一个例子可能对您有帮助。

您可能还想查看 DNR 电视节目 113114

The TransactionStarted is raised by the transaction manager, and the TransactionCompleted by the transaction itself.

You may want the GC to implement IEnlistmentNotification; there is an example that may help you.

You may also want to check DNR TV show 113 and 114.

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