Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Avoiding SQl Server 2008 Deadlocks, Pt 1-Avoiding Cursors

Posted on 2014-01-18
13
Medium Priority
?
656 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 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 53

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 53

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 53

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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