TransactionScope 并发中的死锁问题 [英] TransactionScope deadlocked issue in concurrency

查看:97
本文介绍了TransactionScope 并发中的死锁问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两台不同的服务器上有两个不同的数据库,我使用了 TransactionScope.我第一次使用 TransactionScope.

I have two different databases on two different servers, and I have used a TransactionScope. I'm using TransactionScope for the first time.

但是在更新表1时并发我收到一个错误

事务(进程 ID 64)在锁定资源上与另一个进程发生死锁,并已被选为死锁牺牲品.重新运行事务.

Transaction (Process ID 64) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

在网上查了TransactionScope相关文章

option.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
option.Timeout = TimeSpan.FromMinutes(1);

using (_objTransactionScope = new TransactionScope(TransactionScopeOption.Required, option))
{
    //..Open Connection
    //..Process the Document
    //..Update Additional information in Table2 about document(Delete/Insert)   
    //..Update Location Information in Table3 about document(Delete/Insert)
    //..Delete the Document in Table4 in Differant Database on Differant Server(WCF Service)
    //..Update the Checksum of the Document and Metadata in Table1(Deadlock Error in concurrency)
    //..Delete Lock Entry From Table5   
    //..Close Connection
    //..Commit Transaction Scope
}

推荐答案

当两个用户锁定不同的对象并且每个用户都希望锁定对方的对象时,就会发生死锁.发生这种情况时,SQL Server 通过自动选择一个进程并中止进程来结束死锁,允许另一个进程继续.中止的事务将回滚,并向中止进程的用户发送错误消息.一般来说,需要最少开销回滚的事务就是被中止的事务.

Deadlock occurs when two users have locks on separate objects and each user wants a lock on the other’s object. When this happens, SQL Server ends the deadlock by automatically choosing one and aborting the process, allowing the other process to continue. The aborted transaction is rolled back and an error message is sent to the user of the aborted process. Generally, the transaction that requires the least amount of overhead to rollback is the transaction that is aborted.

解决办法:死锁优先级可由用户设置.换句话说,用户可以选择应该停止哪个进程以允许其他进程继续.SQL Server 自动选择要终止的进程,哪个正在运行完成循环锁链.有时,它会选择运行时间比其他进程短的进程.

Solution: Deadlock priority can be set by user. In other words, user can choose which process should stop to allow other process to continue. SQL Server automatically chooses the process to terminate which is running completes the circular chain of locks. Sometime, it chooses the process which is running the for shorter period then other process.

阅读这篇文章

这篇关于TransactionScope 并发中的死锁问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆