Solved

Avoiding SQl Server 2008 Deadlocks, Pt 1-Avoiding Cursors

Posted on 2014-01-18
13
629 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 52

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
 
LVL 52

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 52

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 52

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 52

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now