Solved

Avoiding SQl Server 2008 Deadlocks, Pt 1-Avoiding Cursors

Posted on 2014-01-18
13
643 Views
Last Modified: 2014-01-19
I am having lots of trouble with deadlocking on my classic asp/sql server 2008 application.  I am trying several things to optimize my query to avoid this.  I am working on avoiding looping through the data and using rs.GetRows() instead.  I also am researching other deadlock causes.  I need some help on avoiding cursors.  Please look at the following and let me know what I can do to further avoid deadlocks:

    Set rs = Server.CreateObject("ADODB.Recordset")
	sql = "SELECT t.TeamsID, t.TeamName, t.Gender FROM Teams t INNER JOIN MeetTeams mt ON t.TeamsID = mt.TeamsID WHERE mt.MeetsID = " & lMeetID & " ORDER BY t.TeamName"
	rs.Open sql, conn2, 1, 2
	MeetTeams = rs.GetRows()
	Set rs = Nothing

Open in new window


Thank you!
0
Comment
Question by:Bob Schneider
[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
  • 7
  • 5
13 Comments
 
LVL 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 400 total points
ID: 39790704
Since you are opening your recordset, sending it to an array then closing it, maybe it would be better to use defaults for cursor and lock types

rs.Open sql, conn2,0,1
0
 

Author Comment

by:Bob Schneider
ID: 39790710
Thanks.  I will try that.
0
 

Author Comment

by:Bob Schneider
ID: 39790802
BTW, I tried the defaults for cursor and lock types and it works well in some cases but in some cases the recordset returns no records.  Do you know if any references I can use to determine when to use the defaults and what they do so I know when I will have to use something else?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 400 total points
ID: 39790863
When you say some cases it returns records and other cases it does not, what does that mean?

You should only be accessing the recordset once and that is to throw it into the array.  After that you should only be accessing data from the array on that page.
0
 

Author Comment

by:Bob Schneider
ID: 39790879
That's what I am doing but it seems like joins are different?  Here is one that did not return any records with the default settings even though they are definitely there:

sql = "SELECT pr.Bib, p.LastName, p.FirstName, p.Gender, pr.Age, ir.FnlTime, p.ParticipantID, ir.ChipStart, p.City, p.St "
sql = sql & "FROM Participant  p INNER JOIN IndResults ir ON p.ParticipantID = ir.ParticipantID "
sql = sql & "INNER JOIN PartRace pr ON pr.ParticipantID = p.ParticipantID "
sql = sql & "INNER JOIN RaceData rd ON rd.RaceID = pr.RaceID AND rd.RaceID = ir.RaceID "
sql = sql & "WHERE ir.RaceID = " & lRaceID & " AND ir.EventPl >= " & iFirstRcd & " AND ir.FnlTime <> '00:00:00.000' ORDER BY ir.EventPl"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 2
If rs.RecordCount > 0 Then
        IndRslts = rs.GetRows()
Else
       ReDim IndRslts(9, 0)
End If
rs.Close
Set rs = Nothing

Open in new window

0
 
LVL 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 400 total points
ID: 39790955
I started to recreate your db and doing something wrong.  Since you know your db better than I do, will you please recreate it and some sample data.

http://sqlfiddle.com/#!3/89603/1
0
 
LVL 5

Assisted Solution

by:rk_india1
rk_india1 earned 100 total points
ID: 39791073
Could you please nolock hint to avid the deadlock situation.
0
 

Author Comment

by:Bob Schneider
ID: 39791550
I don't know what this means: "Could you please nolock hint to avid the deadlock situation."
0
 

Author Comment

by:Bob Schneider
ID: 39791552
If I used the the default lock/cursor, this page, using the query above, would generate no results: http://www.gopherstateevents.com/results/fitness_events/results.asp?event_id=0
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 400 total points
ID: 39791593
As it should.   Unless you have an event ID that has zero as it's id.   But if I try 369, 279 or 348 (by looking at the drop down) I can see data. Interestingly, 340 does not have any results but  it does generate the code below the drop down.   Because of your join's, my guess is when you use event id 0 there is a null in the recordset or it is generating some type of error.  

If you can fill in that sqlfiddle thing, maybe we can get to the bottom of it or this has sparked something?  

I did notice that if I replaced the event_id in the querystring with a letter or negative number I am taken to google, at least you have some type of error checking in place.  When I changed 300 to 3000 I received your turkey's are working on it page.

I wonder if you change this
If rs.RecordCount > 0 Then
        IndRslts = rs.GetRows()
Else
       ReDim IndRslts(9, 0)
End If

Open in new window

to
goodData=0
If rs.RecordCount > 0 Then
        goodData=1
        IndRslts = rs.GetRows()
Else
       ReDim IndRslts(9, 0)
End If

Open in new window

Then in the html where you are presenting results
if goodData=1 then
   ' loop through data
    else
    response.write "That is not a valid event"
end if

Open in new window

0
 

Author Closing Comment

by:Bob Schneider
ID: 39792297
Very helpful.  Thanks!
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39792330
What ended up working?  Did we ever figure out what, "nolock hint to avid the deadlock situation"means?
0
 

Author Comment

by:Bob Schneider
ID: 39792335
I believe that this is a work in progress that will involve me learning more about locks and cursors before I every get to the bottom of it.  I am hoping that, even though I closed this and assigned points, the request for more info on the nolock hint will be supplied.

When I post race results I get hundreds of error messages regarding deadlocking (for CC Meets/Nordic but not for Fitness Events).  I am hoping that eliminating looping and populating arrays with GetRows() will have the biggest effect.

As always, thanks so much for all your knowledge and assistance on things classic asp/sql server
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

759 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