简体   繁体   中英

Transaction Scope Locks table

I am using C# 3.5 and I have a code within transaction scope that is locking the tables , how can I prevent the transaction scope from locking these tables ?

Thanks,

Add a TransactionScope with the option RequireNew and set the IsolationLevel to ReadUncommitted :

using (var t = new TransactionScope(TransactionScopeOption.RequireNew,
new TransactionOptions { 
    IsolationLevel = IsolationLevel.ReadUncommitted 
}))
{
    // your code
}

The TransactionScope makes the code block transactional. The inwolved tables are locked while "code" has not been COMMITED/ROLLED BACK .

您应该使用关键字来防止表锁定,例如nolock,但是要注意如何读取它们,例如读取已提交或未提交读,因为这可能会使您陷入所谓的脏读。

I solved this by adding

with(nolock)

to the SQL stored procedures that are involved in the transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM