When using Linq To SQL, here’s the pattern I use to retry an operation after it failed because a deadlock occurred in a SQL Database:
while (true) {
try {
using (var scope = new TransactionScope()) {
using (var db = new MyDataContext()) {
// Do what you need to do on the DB...
db.SubmitChanges();
}
scope.Complete();
}
break;
}
catch (SqlException e) {
if (e.Number == 1205) {
Thread.Sleep(new Random.Next(1000));
}
else {
throw;
}
}
}