tags:

views:

40

answers:

4

I have a process where two threads are continuously executing sql queries on the same tables. Is there any chance that one of these two threads gets Timeout error? If so, what is the best practice to avoid this?

I am getting following error and resulting is process crash.

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

+1  A: 

There are many reasons you may receive a timeout. If you are getting a connection exception then what is the timeout in the SqlConnection? If not then what is the command timeout in the SqlCommand?

Is your query properly structured? How many rows do you expect your query to return? How many rows are there in the two tables?

Noel Abrahams
@Noel, I am expecting only one row to be returned but there are around 100,000 rows in the table.
Faisal
@Faisal, if that is the case then most likely you have a lock on the table. Run sp_lock while the query is blocking and look for objects with exclusive (X) locks.
Noel Abrahams
+1  A: 

Sounds like you've got a lock on a table. Look into locking mechanisms. If you lock entire table, it's inefficient, instead, you can lock a subset of a table, i.e. - row.

Also in your connection string, you can decrease the timeout. Change it to 2 seconds (time to establish connection, not query data). If you do get that error, than you can work from there.

vikp
These are readlonly operations. Will this help to take a row level locking? I am already using NOLOCK.
Faisal
Is entire table used for reading data, or does it get updated as well?
vikp
Also, if you are reading data during the day, and writing data at night through some sort of automated process, do you need locking at all? As long as reading and writing is seperate and can't occur at the same time, than locking might not be a problem. Is there a limit on how many connections you can establish to your database?
vikp
@vikp,Reading and writing can occur at the same time. I can see a few sql calls in my code that try to update the same table. So, yes i have one thread that is reading and writing to the sql table while other is only reading.
Faisal
@vkip, should I take a rowlock in update statement? Are there any implications. My Update statement will always update one row.
Faisal
I think that setting a lock on a row is very reasonable. Also, where do you acquire locks? In stored procedures or in your application code? I might be wrong, but if you setup locking in stored procedure, you won't have to worry about threads. Please correct me if i'm wrong
vikp
A: 

If your queries are only reading, not updating or inserting, you can use NOLOCKS.

SELECT * FROM MyTable With NOLOCK

This should stop any locking.

Ben
I am already using NOLock
Faisal
A: 

sound like Table locking... you should use a transactionscope with ReadUncommited option something like:

var islolation=new TransactionOptions();
isolation=IsolationLevel.ReadUncommitted;
using(var scope=new TransactionScope(TransactionScopeOption.requiresnew,isolation))
{
//code here
scope.complete();
}

also it would be nice to check if you are closing connection right after query execution.

Usman Masood