.net检测分布式事务

发布于 2024-10-04 12:23:17 字数 532 浏览 0 评论 0原文

在我的应用程序中,我使用以下模式来调用数据库:

    //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 技术交流群。

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

发布评论

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

评论(3

谁人与我共长歌 2024-10-11 12:23:17

查看DistributedTransactionPermissionAttribute。它使用 DistributedTransactionPermission 类,该类是当事务管理升级到 MSDTC(来自文档)时 System.Transactions 所需的权限。

您可以将其应用到您的代码中。应在升级时提出安全例外。

Have a look at the DistributedTransactionPermissionAttribute. It's using the DistributedTransactionPermission 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.

时光是把杀猪刀 2024-10-11 12:23:17

当您的 TransactionScope 正在进行时,您可以测试:

Transaction.Current.TransactionInformation.DistributedIdentifier != Guid.Empty

如果事务尚未升级为分布式,DistributedIdentifierEmpty。从其文档,备注部分:

如果事务升级为两阶段提交事务,则此属性返回其唯一标识符。如果交易未升级,则值为Empty

While your TransactionScope is ongoing, you may test:

Transaction.Current.TransactionInformation.DistributedIdentifier != Guid.Empty

DistributedIdentifier is Empty if the transaction is not yet promoted to distributed. From its documentation, remarks section:

If the transaction is escalated to a two-phase commit transaction, this property returns its unique identifier. If the transaction is not escalated, the value is Empty.

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