.NET:由于事务超时而异步回滚
在 C# 项目中,我在 System.Transactions.Transaction
中登记了一个(易失性)事务资源。
当事务超时时,回滚是在工作线程中执行的:显然,事务使用计时器,并在计时器到时调用计时器回调(Microsoft关于这个问题的文档很少) 。 因此,我的事务资源中的 IEnlistmentNotification.Rollback(...
) 的实现是异步调用的。
我的问题是:我该如何处理这个问题? 当然,回滚代码会更改我的资源的内部状态,例如通过丢弃更改。 看来我必须同步对该资源的所有访问。 然而,该资源并不是被设计为线程保存的。 有没有人有解决这个问题的经验,或者是否有某种“最佳实践”?
In a C# project, I have a (volatile) transactional resource enlisted in a System.Transactions.Transaction
.
When a transaction timeout occurs, the rollback is executed in a worker thread: Obviously, the transaction uses a timer and calls a timer callback when the timer elapses (there is very little documentation from Microsoft on this issue). Thus, the implementation of IEnlistmentNotification.Rollback(...
) in my transactional resource is called asynchronously.
My question is: How do I handle this? Of course, the rollback code changes the internal state of my resource, e.g. by discarding changes. It seems that I must synchronize all access to this resource. However, the resource is not designed to be thread save. Does anyone have experience with this problem, or is there some kind of "best practice" for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你可以锁定资源的回滚方法以使它们同步,因为回滚将是奇怪的情况并且序列化访问不会导致太多性能问题...
使用 IoC,例如 Windsor,实现 IInterceptor并使用拦截器锁定所有 IResource.Rollback() 方法。 这是一个正交问题,因此应该作为一个整体来实现。
I guess you could lock the resources' rollback methods to make them synchronized, as the rollback would be the odd-case-out and serializing access wouldn't cause too much problem perf-wise...
With IoC, e.g. Windsor, implement IInterceptor and lock around all IResource.Rollback() methods with the interceptor. It's an orthogonal concern, so it should be implemented as one.