W aplikacji używam EF i SQL .W serwisie WCF w metodzie której zadaniem jest pobranie czegoś z bazy i edytowanie a następnie zapisanie transakcji, raz na kilkadziesiąt wywołań pojawia się ten błąd:
//Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction
//
Struktura metody jest taka:
public void method()
{
using (var context = new dataContext())
{
var transaction = context.DatabaseBeginTransaction();
try
{
//bla bla bla
transaction.Commit();
}
catch(Exception ex)
{
Logger.Error("Error", ex);
if (transaction != null)
transacttion.Rollback();
}
}
}
Za każdym razem jeżeli wywali ten wyjątek i wejdzie w catch wywala kolejny wyjątek na metodzie Rollback (The underlying provider failed on Rollback).
Okazuje się że Rollback mogę się chyba pozbyć bo "DbContextTransaction.Dispose method will be called in the end of the using block"
Tylko ciekawe czemu mi na tym wyjątek leci.
W takim razie to rozwiązanie powinno pomóc?
int retryCount = 3;
bool success = false;
while (retryCount > 0 && !success)
{
try
{
// your sql here
success = true;
}
catch (SqlException exception)
{
if (exception.Number != 1205)
{
// a sql exception that is not a deadlock
throw;
}
// Add delay here if you wish.
retryCount--;
if (retryCount == 0) throw;
}
}
lub coś takiego:
dataContext.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
Macie lepszy pomysł na rozwiązanie tego problemu ?