.net检测分布式事务
在我的应用程序中,我使用以下模式来调用数据库:
//do a transaction
using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required))
{
OperationOnDb1();
//when we open the connection to the “other db” in this call, the transaction would become distributed
OperationOnDb2();
//transaction is now distributed
transaction.Complete();
}
问题是Operation1和Operation2 90%的时间使用相同的数据库......但是当它们使用两个数据库时存在一些情况(错误)。如果交易变得分布式,我想得到一个例外。
如何检测事务是否升级为分布式事务?
谢谢,拉杜
In my application I use the following pattern for calling the DB:
//do a transaction
using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required))
{
OperationOnDb1();
//when we open the connection to the “other db” in this call, the transaction would become distributed
OperationOnDb2();
//transaction is now distributed
transaction.Complete();
}
The problem is that Operation1 and Operation2 90% of the time use the same db ... but there are cases (bugs) when they use two DBs. I want to get an exception if the transaction becomes distributed.
How can I detect if the transaction is promoted to a distributed transaction?
Thanks, Radu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以查看以下事件
TransactionManager.DistributedTransactionStarted活动
You can also have a look at the following event
TransactionManager.DistributedTransactionStarted Event
查看
DistributedTransactionPermissionAttribute
。它使用 DistributedTransactionPermission 类,该类是当事务管理升级到 MSDTC(来自文档)时 System.Transactions 所需的权限。您可以将其应用到您的代码中。应在升级时提出安全例外。
Have a look at the
DistributedTransactionPermissionAttribute
. It's using theDistributedTransactionPermission
class wich is the permission that is demanded by System.Transactions when management of a transaction is escalated to MSDTC (from doc).You could apply it to your piece of code. A security exception should be raised on escalation.
当您的
TransactionScope
正在进行时,您可以测试:如果事务尚未升级为分布式,
DistributedIdentifier
为Empty
。从其文档,备注部分:While your
TransactionScope
is ongoing, you may test:DistributedIdentifier
isEmpty
if the transaction is not yet promoted to distributed. From its documentation, remarks section: