473,786 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Execute Reader()
.....

' No exception on execute, so we were not deadlocked

bDeadlocked = False

Catch Ex As Exception

If Ex.Message.Cont ains("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.Error s.SourceGeneral Exception)

End If

End If

Return New DataError(Ex, DataError.Error s.SourceGeneral Exception)

Finally

.....

End Try

End While


Aug 24 '06 #1
1 3612
.... 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.B eginTransaction ()

.... Result = Foo ( theConnection )

.... Result = Bar ( theConnection )

.... theConnection.C ommit ()

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.B eginTransaction ()

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

.... Result = Bar ( theConnection )
If Result = MyErrorType.Dea dLocked Then

bDeadlocked += 1

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

throw new Exception ( ""Rerun the transaction")

End If

.... theConnection.C ommit ()
Catch sqlEx As Exception
End Try

...
End While
>


' DEADLOCK PREFIX

Dim bDeadlocked As Boolean = True, nDeadlocks As Integer = 0

While bDeadlocked

Try
.....
DataReader = Command.Execute Reader()
.....

' No exception on execute, so we were not deadlocked

bDeadlocked = False

Catch Ex As Exception

If Ex.Message.Cont ains("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.Error s.SourceGeneral Exception)

End If

End If

Return New DataError(Ex, DataError.Error s.SourceGeneral Exception)

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
6078
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 load. The statement is DELETE from userlogins WHERE numlogins <= 0 the schema for the userlogins table is
1
4003
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 the New Alert error number property for a deadlock. Or how to specify a deadlock on a specific table. Thanks, DW
7
9228
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 a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
3
7628
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 file_uid / obj_uid. The isolation level is UR and I have set DB2_RR_TO_RS=YES. Any thoughts why I'm getting the deadlock ?
1
4243
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. From the log it looks like the problem happens when 2 threads insert 1 record each in the same table and then try to aquire a NS (Next Key Share) lock on the record inserterd by the other thread. Thanks Rohit
15
10005
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) 3-4 times and give up if after that it's still in deadlock. I'm very sure that many experienced people out there already deal with this issue somehow. Is there an alternative to it? Thanks for your comments and suggestions.
1
3689
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 "access_log", the referential integrity triggers generate these queries: SELECT 1 FROM ONLY "public"."application_type" x
2
8968
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 I understand is to wait for however long it takes to acquire a lock. Also there are other processes that have acquired row level exclusive locks on the table A when this error happened.Is DB2 throwing this error as a pre-emptive measure.
0
11704
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 java.sql.SQLException: Deadlock found when trying to get lock; Try restarting transaction message from server: "Lock wait timeout exceeded; try restarting transaction"; We get such errors generally on inserts or updates while applying a
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.