views:

363

answers:

13

Hey Guys We got a legacy vb.net applicaction that was working for years But all of a sudden it stops working yesterday and gives sql server timeout Most part of application gives time out error , one part for example is below code :

command2 = New SqlCommand("select * from Acc order by AccDate,AccNo,AccSeq", SBSConnection2)
        reader2 = command2.ExecuteReader()
    If reader2.HasRows() Then
        While reader2.Read()
            If IndiAccNo <> reader2("AccNo") Then
                CAccNo = CAccNo + 1
                CAccSeq = 10001
                IndiAccNo = reader2("AccNo")
            Else
                CAccSeq = CAccSeq + 1
           End If
            command3 = New SqlCommand("update Acc Set AccNo=@NewAccNo,AccSeq=@NewAccSeq where AccNo=@AccNo and AccSeq=@AccSeq", SBSConnection3)
            command3.Parameters.Add("@AccNo", SqlDbType.Int).Value = reader2("AccNo")
        command3.Parameters.Add("@AccSeq", SqlDbType.Int).Value = reader2("AccSeq")
          command3.Parameters.Add("@NewAccNo", SqlDbType.Int).Value = CAccNo
        command3.Parameters.Add("@NewAccSeq", SqlDbType.Int).Value = CAccSeq

      command3.ExecuteNonQuery()
    End While
End If

It was working and now gives time out in command3.ExecuteNonQuery() Any ideas ?

~~~~~~~~~~~ Some information :

There isnt anything that has been changed on network and the app uses local database The main issue is that even in development environment it donest work anymore

A: 

Can we make sure that the SQLConnection is working fine. It could be the case that SQL login criteria is changed and connection is getting a timeout. It will be probably more helpful if you post the error message here.

ahmjt
SQLConnection is fine , actually in a place where we got reader loop we got timeout message
Adinochestva
Your comment suggests an increase in data records. So far HLGEM's answer seems your best shot.
AMissico
+1  A: 

any chances of a "quotes" as part of the strings you are passing to queries?

any chances of date dependent queries where a special condition is not working anymore?

A.Rashad
+3  A: 

I would suggest that there is a lock on one of the records that you are trying to update, or there are transactions that haven't been completed.

I know this is not part of your question, but after seeing your sample code i have to make this comment: is there any chance you could change your method of executing sql on your database? It is bad on so many levels.

slugster
I agree, you're reading and updating the same table. It could be waiting for the select lock to finish so it can do the update, but the select is waiting for the update so it can continue reading onwards.Take the full recordset into memory and then iterate that. Better yet make it a stored procedure as noted above.Software deadlock.
Meff
+14  A: 

I'll state the obvious - something changed. It could be an upgrade that isn't having the desired effect - it could be a network component going south - it could be a flakey disk - it could be many things - but something in the access path has changed. What other problem indications are you seeing, including problems not directly related to this application? Where is the database stored (local disk, network storage box, written by angels on the head of a pin, other)? Has your system administrator "helped" or "improved" things somehow? The code has not worn out - something else has happened.

Bob Jarvis
'written by angels on the head of a pin' - hilarious :)
Henrik Opel
+14  A: 

Is it possible that this query has been getting slower over time and is now just exceeded the default timeout?

How many records would be in the acc table and are there indexes on AccNo and AccSeq? Also what version of SQL are you using?

sgmoore
A: 

You can rewrite the update as a single query. This will run much faster than the original query.

UPDATE subquery
SET AccNo = NewAccNo, AccSeq = NewAccSeq
FROM
    (SELECT AccNo, AccSeq, 
        DENSE_RANK() OVER (PARTITION BY AccNo ORDER BY AccNo) NewAccNo,
        ROW_NUMBER() OVER (PARTITION BY AccNo ORDER BY AccDate, AccSeq)
            + 10000 NewAccSeq
     FROM Acc) subquery
Anthony Faull
Are you sure about the part DENSE_RANK() OVER (PARTITION BY AccNo ORDER BY AccDate) NewAccNo ?
sgmoore
@sgmoore Good catch. I've changed it order by AccNo.
Anthony Faull
Nope. I still don't get the results I would expect. By the way, I don't know how to easily do this, which is why I was playing with this to see how it works.
sgmoore
I've just updated the query. See if it works now.
Anthony Faull
+10  A: 

How long since you updated statistics and rebuilt indexes?

How much has your data grown? Queries that work fine for small datasets can be bad for large ones.

Are you getting locking issues? [AMJ] Have you checked activity monitor to see if there are locks when the timeout occurs?

Have you run profiler to grab the query that is timing out and then run it directly onthe server? Is it faster then? Could also be network issues in moving the information from the database server to the application. That would at least tell you if it s SQl Server issue or a network issue.

And like Bob Jarvis said, what has recently changed on the server? Has something changed in the database structure itself? Has someone added a trigger?

HLGEM
+1 for update statistics and rebuild indexes. Also, using profiler you can see if you need to change/add indexes because of table size growth.
Jim
+1 this should be the first thing checked. Good answer, covers the basic first-aid troubleshooting steps. Do not change the code. Find out why this is failing.
AMissico
+1  A: 

Perhaps should you set the CommandTimeout property to a higher delay?

Doing so will allow your command to wait a little longer for the underlying database to respond. As I see it, perhaps are you not letting time enough for your database engine to perform all what is required before creating another command to perform your update.

Know that the SqlDataReader continues to "SELECT" while feeding the in-memory objects. Then, while reading, you require your code to update some other table, which your DBE just can't handle, by the time your SqlCommand requires, than times out.

Will Marcouiller
I see this as only a temporary fix to get prod working again while you track down the real problem causing the time-out. I wouldn't support it as the permanent fix as something is causing the problem and it will porbably re-occur until you fix it.
HLGEM
A: 

After HLGEM's suggestions, I would check the data and make sure it is okay. In cases like this, 95% of the time it is the data.

AMissico
A: 

Make sure disk is defragged. Yes, I know, but it does make a difference. Not the built-in defragger. One that defrags and optimizes like PerfectDisk.

AMissico
To the person who down-voted. If all you have is gaps on a heavily used hard-drive, I do not care how optimized SQL Server is...the page files are all over the place and performance suffers terribly. Twice before using PerfectDisk has noticeabily increased SQL Server response time and shorten query execution times.
AMissico
+1  A: 

Have you tested the obvious?

Have you run the "update Acc Set AccNo=@NewAccNo,AccSeq=@NewAccSeq where AccNo=@AccNo and AccSeq=@AccSeq" query directly on your SQL Server Management Studio? (Please replace the variables with some hard coded values)

Have you run the same test on another colleague's PC?

Syd
A: 

This may be a bit of a long shot, but if your entire application has stopped working, have you run out of space for the transaction log in your database? Either it's been specified to an absolute size, and that has been reached, or your disk is just full.

Paddy
A: 

May be your tables include more information, and defined SqlConnection.ConnectionTimeout property value in config file with little value. And this value isn't necessary to execute your queries. you can trying optimize your queries, and also rebuilt indexes.

loviji