catching and handling SQL deadlocks from vb.net

Posted on 2008-06-18
Medium Priority
Last Modified: 2013-11-26
I'm using vb.net 2005 and SQL 2005. Very occasionally I am getting a SQL deadlock - "Transaction (Process ID 258) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction."

- how do I check for the deadlock exception in my catch block?
- I am handling the transaction from .NET so I do a trans.commit or trans.rollback. However, in some situations SQL does the rollback itself - in deadlock situations for example. How can I check that my transaction is still 'active' before I attempt to rollback?

            Using tran As SqlTransaction = 						cn.appConnection.BeginTransaction(IsolationLevel.ReadCommitted)	
                    If objtransMasterList.IsDirty Then
                        Dim objTransMaster As SQLTransMaster
                        For Each objTransMaster In objtransMasterList
                            If objTransMaster.IsNew Then
                                Dim cmd As New SqlCommand
                                cmd.CommandText = SQLInsertStoredProc
                                cmd.Connection = cn.appConnection
                                cmd.Transaction = tran
                                cmd.CommandType = CommandType.StoredProcedure
                                saveTransMasterParamsFromObj(cmd, CMDTYPES.INSERT, objTransMaster)
                            End If
                    End If
                Catch a As Exception
                    success = False
                    GoTo finally_Renamed
                End Try
            End Using
        Catch b As Exception
            success = False
            If Not cn.appConnection Is Nothing Then
                If cn.appConnection.State <> ConnectionState.Closed Then cn.CloseConnection()
            End If
        End Try

Open in new window

Question by:darrenwright
  • 2

Expert Comment

ID: 21813055
Where did you see ""Transaction (Process ID 258) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.""?

was that in the event log? or can you see this in the exception thrown?

Author Comment

ID: 21813290
in my catch block I process the error via objSQLErrorList.processError which writes the error to a log file.
of my two part question, I now think I've sorted the first one - I'm just testing it now. I've changed so I catch the sqlexception and check for number 1205. What I need to work out now is second part of question. How can I check whether I need to do a trans.rollback or whether it's already been rolled back by SQL?  

Catch sqlEx As SqlException
    success = False
     If sqlEx.Number = 1205 Then 'deadlock
              mustRetry = True
               retryCount = retryCount + 1S
      End If

Accepted Solution

Chrisedebo earned 1500 total points
ID: 21813345
Is the tran.rollback() throwing an exception when there is nothing to roll back? if thats the case, put it inside a try/catch block and ignore the error. If there's nothing to rollback (ie the trans.commit executed sucessfully or sql server rolls back automatically like for deadlocks) then it won't roll anything back.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question