R
Robinson
Apologies for the cross post, but I'm not too sure which group this belongs
in. At least I didn't get responses in the MSDE groups yet. Anyway what I
want to do is create a simple pattern for dealing with deadlock situations
in my VB.NET program. So far, I've come up with something like this
(below). The idea is simply to detect the deadlock exception and iterate
the operation until it finally succeeds (or we've tried it 3 times). Am I
missing something really important here, or is this an acceptable way to
deal with it? Alternatively I could just ignore the dead lock, signal to
the user as usual that an error occurred and carry on. I'm not sure what
the best way of dealing with these situations is.
Thanks.
Robin
' DEADLOCK PREFIX
Dim bDeadlocked As Boolean = True, nDeadlocks As Integer = 0
While bDeadlocked
Try
.....
DataReader = Command.ExecuteReader()
.....
' No exception on execute, so we were not deadlocked
bDeadlocked = False
Catch Ex As Exception
If Ex.Message.Contains("Rerun the transaction") Then
' We failed through deadlock.
nDeadlocks += 1
If nDeadlocks = 3 Then
' We deadlocked 3 times before, so give up now.
Return New DataError(Ex,
DataError.Errors.SourceGeneralException)
End If
End If
Return New DataError(Ex, DataError.Errors.SourceGeneralException)
Finally
.....
End Try
End While
in. At least I didn't get responses in the MSDE groups yet. Anyway what I
want to do is create a simple pattern for dealing with deadlock situations
in my VB.NET program. So far, I've come up with something like this
(below). The idea is simply to detect the deadlock exception and iterate
the operation until it finally succeeds (or we've tried it 3 times). Am I
missing something really important here, or is this an acceptable way to
deal with it? Alternatively I could just ignore the dead lock, signal to
the user as usual that an error occurred and carry on. I'm not sure what
the best way of dealing with these situations is.
Thanks.
Robin
' DEADLOCK PREFIX
Dim bDeadlocked As Boolean = True, nDeadlocks As Integer = 0
While bDeadlocked
Try
.....
DataReader = Command.ExecuteReader()
.....
' No exception on execute, so we were not deadlocked
bDeadlocked = False
Catch Ex As Exception
If Ex.Message.Contains("Rerun the transaction") Then
' We failed through deadlock.
nDeadlocks += 1
If nDeadlocks = 3 Then
' We deadlocked 3 times before, so give up now.
Return New DataError(Ex,
DataError.Errors.SourceGeneralException)
End If
End If
Return New DataError(Ex, DataError.Errors.SourceGeneralException)
Finally
.....
End Try
End While