开发者

TransactionScope and deadlocks in sproc

开发者 https://www.devze.com 2023-03-17 00:33 出处:网络
I\'ve got a sproc that basically goes: begin transaction 开发者_Go百科SELECT UPDATE INSERT commit transaction

I've got a sproc that basically goes:

begin transaction
    开发者_Go百科SELECT
    UPDATE
    INSERT
commit transaction

This sproc is called inside a loop from two different threads in my application, both within a TransactionScope with default options.

Occasionally, my application deadlocks:

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

Is there anything I can do about this? Should I use a different isolation level?


Default isolation level used by TransactionScope is Serializable which usually is unnecessary. Use a constructor which takes TransactionOptions to specify other level.

Example based on blog post from David Baxter Browne (a great blog post describing this problem).

TransactionOptions tOptions= new TransactionOptions();
tOptions.IsolationLevel = IsolationLevel.ReadCommitted;
using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, tOptions))
{
  // do stuff here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消