对于TADOQuery、TADOCommand或TADODataSet查询执行超时的设置,我有问题(我已经对每个设置都尝试过了)。我有一个小应用程序,它连接到数据库,周期性地执行存储过程,它返回数据集作为结果。我的目标是让这个应用程序始终在线,但我的问题是当连接丢失时,只执行命令的超时(通过上述组件之一)会占用默认的30秒。我一直在寻找解决办法,但什么都没有。你能给我一个建议吗?如何将CommandTimeout设置为5秒或更好,比如如何修改ADODB.pas以尊重我自己的超时?
这方面有许多“解决方案”,比如set DataComponent.Connection.CommandTimeout := 1;但实际上,什么都不管用。我使用D2009、MSSQL2005和连接以及数据组件是动态地在线程中创建的。
最后一个,我试过的是这个
// 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
...非常感谢:)
发布于 2011-02-22 22:29:06
当您有长期运行的查询时,CommandTimeout正在启动。有一个CommandTimeout属性TADOConnection,但不起作用。您必须使用CommandTimeout的TADODataSet代替。
如果服务器不可用,您的问题是“连接丢失”,您需要指定TADOConnection组件的TADOConnection。默认情况下,在将控件返回给应用程序之前15秒。
编辑1我想我发现了CommandTimeout不起作用的情况。我在一张很大的桌子上测试了这个。返回所有行需要几分钟。如果我的存储过程执行select * from BigTable,查询超时永远不会发生。至少我没有足够的耐心等下去。但是,如果查询看起来像这个select * from BigTable order by Col1,而且Col1上没有索引,那么CommandTimout就会像预期的那样工作。
在SSMS中运行这两个查询时,这两个查询之间的区别是显而易见的。第一个开始立即返回行,第二个需要在返回行之前“考虑”它。当Server找到它需要的行并开始返回它们时,CommandTimeout无法工作。
如果您将CursorLocation设置为clUseServer,那么对于这两个查询,CommandTimeout都会像预期的那样工作。
发布于 2011-02-22 10:59:34
下面是我们将长期运行报告的超时设置为300的内容。
//***** Fix setting CommandTimeOut.
// CommandTimeOut "should" get the timeout value from its connection.
// This is not supported in ADODB (using Delphi5)
TADODataSet(qryReport).CommandTimeout := ADOConnection.CommandTimeout;编辑
在我的开发机器上执行下面的代码在1秒后超时。
测试
procedure TForm1.btn1Click(Sender: TObject);
const
SSQL: string =
'DECLARE @intLoop int '#13#10
+ 'SET @intLoop = 10 '#13#10
+ 'WHILE @intLoop > 1 '#13#10
+ 'BEGIN '#13#10
+ ' SELECT @intLoop, GetDate() '#13#10
+ ' WAITFOR DELAY ''00:00:01'' '#13#10
+ ' SELECT @intLoop = @intLoop -1 '#13#10
+ 'END ';
begin
qry1.SQL.Text := SSQL;
TADODataSet(qry1).CommandTimeout := 1;
qry1.ExecSQL;
end;发布于 2011-02-22 11:51:32
我一直使用以下代码来设置CommandTimeout值在TADOQuery上。如果您调整了类名,那么它也应该与其他类一起工作。
type
TADOQueryHack = class(TADOQuery);
...
TADOQueryHack(Qry).CommandTimeout := COMM_TIMEOUT;https://stackoverflow.com/questions/5077051
复制相似问题