Solved

catching and handling SQL deadlocks from vb.net

Posted on 2008-06-18
3
3,212 Views
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?


Try
            cn.startUPConnection()
            Using tran As SqlTransaction = 						cn.appConnection.BeginTransaction(IsolationLevel.ReadCommitted)	
                Try
                    
                    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)
      
                                cmd.ExecuteNonQuery()
                            End If
                        Next
                    End If
                    tran.Commit()
                Catch a As Exception
                    tran.Rollback()
                    success = False
                    objSQLErrorList.processError(a)
                    unLockIt()
                    GoTo finally_Renamed
                End Try
            End Using
 
        Catch b As Exception
            success = False
            objSQLErrorList.processError(b)
            unLockIt()
        Finally
            If Not cn.appConnection Is Nothing Then
                If cn.appConnection.State <> ConnectionState.Closed Then cn.CloseConnection()
            End If
        End Try

Open in new window

0
Comment
Question by:darrenwright
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Chrisedebo
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?
0
 

Author Comment

by:darrenwright
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
                       
0
 
LVL 7

Accepted Solution

by:
Chrisedebo earned 500 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.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

717 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