Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Avoiding SQl Server 2008 Deadlocks, Pt 1-Avoiding Cursors

Posted on 2014-01-18
13
Medium Priority
?
663 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
  • 7
  • 5
13 Comments
 
LVL 54

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 1600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 54

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 1600 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 54

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 1600 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 400 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 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 1600 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 54

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

886 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