在我的应用程序中,我使用以下模式调用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();
}问题是Operation1和Operation2 90%的时间使用相同的数据库...但是,当它们使用两个DB时,也会出现一些情况(Bug)。如果事务变得分布式,我想得到一个异常。
如何检测事务是否被提升为分布式事务?
谢谢你,拉度
发布于 2010-11-25 16:55:02
您还可以查看以下事件
TransactionManager.DistributedTransactionStarted Event
发布于 2010-11-25 16:49:22
看一看DistributedTransactionPermissionAttribute。它使用的是DistributedTransactionPermission类,当事务的管理升级到MSDTC (来自文档)时,这是System.Transactions所要求的权限。
您可以将其应用于您的代码片段。升级时应引发安全异常。
发布于 2017-05-13 02:05:53
当您的TransactionScope正在进行时,您可以测试:
Transaction.Current.TransactionInformation.DistributedIdentifier != default(Guid)如果事务尚未升级到分布式,则称DistributedIdentifier为null。从它的文档,备注部分:
如果事务升级为两阶段提交事务,则此属性返回其唯一标识符。如果事务未升级,则值为
null。
由于此属性不可为空,因此这在原则上显然是错误的。但是通过ILSpy检查,当事务不是分布式的时,它是Guid.Empty (这是default(Guid))。(但可能某些非MSDTC分布式事务不会考虑这一点。)
https://stackoverflow.com/questions/4274887
复制相似问题