Patterns for handling a SQL deadlock in C#?

后端 未结 2 1479
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 06:49

I\'m writing an application in C# which accesses a SQL Server 2005 database. The application is quite database intensive, and even if I try to optimize all access, set up proper

2条回答
  •  花落未央
    2021-02-19 07:25

    I posted a code sample to handle exactly this a while back, but SO seemed to lose my account in the interim so I can't find it now I'm afraid and don't have the code I used here.

    Short answer - wrap the thing in a try..catch. If you catch an error which looks like a deadlock, sleep for a short random time and increment a retry the counter. If you get another error or the retry counter clears your threshold, throw the error back up to the calling routine.

    (And if you can, try to bung this in a general routine and run most/all of your DB access through it so you're handling deadlocks program-wide.)

    EDIT: Ah, teach me not to use Google! The previous code sample I and others gave is at How to get efficient Sql Server deadlock handling in C# with ADO?

提交回复
热议问题