Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Created January 15, 2013 15:33
Show Gist options
  • Save i-e-b/4539479 to your computer and use it in GitHub Desktop.
Save i-e-b/4539479 to your computer and use it in GitHub Desktop.
Sql deadlock handler
public class With
{
const int SqlDeadlockErrorNumber = 1205;
const int RetryLimit = 10;
public static T DeadlockRetry<T>(Func<T> thingToTry)
{
var lastError = new Exception("Unknown state");
for (int i = 0; i < RetryLimit; i++)
{
try
{
return thingToTry();
}
catch (SqlException error)
{
if (error.Number != SqlDeadlockErrorNumber) throw;
lastError = error;
}
}
throw lastError;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment