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
}
精彩评论