Solved

Avoiding SQl Server 2008 Deadlocks, Pt 1-Avoiding Cursors

Posted on 2014-01-18
13
649 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

626 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