ADO components CommandTimeout

user532231 picture user532231 · Feb 22, 2011 · Viewed 15.6k times · Source

I have a problem with settings of the query execution timeout with TADOQuery, TADOCommand or TADODataSet (I've tried it with each one). I have a tiny application, which connects to the database and periodically executes stored procedures, which returns dataset as a result. My aim is to keep this application always online, but my problem is that when the connection is lost, the timeout of just executed command (through the one of the mentioned components) takes the default 30 seconds. I've been looking for the solution, but nothing works. Could you give me an advice, how to set the CommandTimeout e.g. to 5 seconds or better to say how to modify ADODB.pas for respecting my own timeout, please ?

There were many "solutions" for this, like set DataComponent.Connection.CommandTimeout := 1; but really, nothing works. I'm using D2009, MSSQL2005 and the connection along with the data component is created dynamically in the thread.

The last, what I've tried is this

// protected variable owned and created in the thread with its own connection
var Query_Object: TADODataSet; 

// connection timeout is set to 3 seconds
Query_Object.Connection.ConnectionTimeout := 3;
...

// this piece of code I'm calling periodically in the only one existing thread
...
SQL_Query := 'EXEC my_procedure_which_returns_dataset'

with Query_Object do
  begin
    Close;    
    CommandType := cmdText;
    CommandText := SQL_Query;
    CommandTimeout := 5;             // doesn't affect the timeout
    CursorLocation := clUseServer;   // let the dataset retreives prepared data
    Open;
  end;

// and here I need to get faster than in the default 15 seconds to let the user
// know that the reading takes more than mentioned 5 seconds
...

Thanks a lot :)

Answer

Mikael Eriksson picture Mikael Eriksson · Feb 22, 2011

CommandTimeout is kicking in when you have long running queries. There is a CommandTimeout property of TADOConnection but that does not work. You have to use the CommandTimeout of the TADODataSet instead.

If the server is unavailable, your question says "connection is lost", you need to specify ConnectionTimeout of the TADOConnection component. Default is 15 seconds before control is returned to your application.

Edit 1 I think I have discovered a situation where CommandTimeout does not work. I have tested this against a really big table. It takes several minutes to return all rows. If my stored procedure does select * from BigTable the query timeout never happens. At least I was not patient enough to wait it out. But if the query looks like this select * from BigTable order by Col1 and there is no index on Col1, the CommandTimout works as expected.

The difference between the two queries is obvious when running them in SSMS. The first starts to immediately return rows and the second needs to "think" about it before it returns rows. When SQL Server have found the rows it needs and start to return them, CommandTimeout does not work.

If you set CursorLocation to clUseServer the CommandTimeout will work as expected for both queries.