473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Deadlock Patterns....

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


Aug 24 '06 #1
1 3590
.... and another interesting question, where will the deadlock exception
occur, if I have, say, one transaction block and execute two (for example)
stored procedures on it? i.e.:

Try

.... theConnection.BeginTransaction ()

.... Result = Foo ( theConnection )

.... Result = Bar ( theConnection )

.... theConnection.Commit ()

Catch sqlEx As Exception

End Try
Presumably either of Foo and Bar can fail due to resource deadlock on the
DBMS. This being the case, do they handle the exception and re-try their
part of the transaction, or do I have to unroll the entire transaction and
start again? In other words, what is the granularity of resource deadlocks?
Might I have to write:

while bDeadlocked
Try

.... theConnection.BeginTransaction ()

.... Result = Foo ( theConnection )
If Result = MyErrorType.Deadlocked Then
bDeadlocked += 1
throw new Exception ( ""Rerun the transaction")
End If

.... Result = Bar ( theConnection )
If Result = MyErrorType.DeadLocked Then

bDeadlocked += 1

.... unwind any work that Foo has done...

throw new Exception ( ""Rerun the transaction")

End If

.... theConnection.Commit ()
Catch sqlEx As Exception
End Try

...
End While
>


' 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


Aug 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Brown | last post by:
I have a deadlock that's happening on one oracle instance but cannot be reproduced on any other. It is always caused by the same SQL statement colliding with itself and only happens under very high...
1
by: dawatson833 | last post by:
I want to set an alert for a specific table whenever an event has caused a deadlock to occur on the table. I understand how to set up an alert. But I don't know which error number to use for...
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
3
by: Nigel Robbins | last post by:
Hi There, I'm getting a deadlock when I have two clients running the following statement. DELETE FROM intermediate.file_os_details WHERE file_uid = ? AND obj_uid There is a compound index on...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
15
by: Zeng | last post by:
Hi, The bigger my C# web-application gets, the more places I need to put in the tedious retrying block of code to make sure operations that can run into database deadlocks are re-run (retried)...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
2
by: Sumanth | last post by:
Hi , I am trying to acquire a lock on a table A in exclusive mode, and this statement gives an error indicating a deadlock or timeout has been detected. The lock timeout value is set to 0 which...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.