Re: ADOConnection fail after a while in TThread
- From: Tom Hagen <tom.hagen@xxxxxxxxxx>
- Date: Mon, 17 Oct 2005 21:11:52 +0200
Hi Mikael,
I, as Martin, do not think it is wise to use so much of objects (both ADO and thread), Nevertheless you might increase the socket count etc.
If I may, I would suggest you a general design change (partly mentioned by Martin). Concretely:
1. If you do not use Transactions and have only one Query per Connection you even do not need the Connection. Simply assign Connection string directly for the ADO Query. As a result you might get a slight increase of speed and slight memory usage decrease.
2. You do not need to use separate threads for each of your connections. Take a deeper look at TADOQuery.ExecuteOptions. If you use the eoAssync... values, the threads are created for you by ADO directly in the background. It needs a little more attention, 'cause the Open method returns immediatelly, and the data are still not loaded (use Events to get notified and perhaps create the Threads for your "do some work" then). On the other hand it gives you a chance to call Open for all the Queries "at once" as you require.
3. The simpliest the best :) Solve the problem with "not permanent" connection to your stores. It might be the most suitable solution for your problems... :)
Since you didn't post whats is the "do some work", these suggestions might be useless ...
Tom
Mikael Lenfors wrote:
Hello!
I have a problem when using ADO components in a thread. My program starts about 150 threads for communication with our stores. Each thread creates it's own ADOConnection and ADOQuery components. When all 150 threads have terminated it starts all over again (creating 150 new threads). All this works perfectly until suddenly (after about 20-100 cycles) it fails to connect to the store databases and in every thread I get an exception in ConnectDB. Below I show the essential part of the code. If I stop the program and restart it, it works fine again for 20-100 cycles.
One strange thing is that I have a ADOConnection in the main thread as well. When this thread problem occures also this ADOConnection fail to connect, to a completely different datasource.
Any ideas?
Regards, Mikael
------------
Type
TMyThread = class(TThread)
private
ADOConnection: TADOConnection;
ADOQuery: TADOQuery;
------------
Procedure TMyThread.Execute;
Begin
ADOConnection := TADOConnection.Create(Nil); <--- is it Ok to use Nil here?
ADOQuery := TADOQuery.Create(Nil);
ADOQuery.Connection := ADOConnection;
If ConnectDB Then
Begin
----Do some work with the ADOQuery
ADOQuery.Close;
ADOConnection.Close;
End;
ADOQuery.Free;
ADOConnection.Free;
End;
------------
Function TMyThread.ConnectDB: Boolean;
Begin
Result := False;
Try
ADOConnection.Close;
ADOQuery.Close;
ADOQuery.CursorType := ctOpenForwardOnly;
ADOQuery.CommandTimeout := 5 * 60; // 5 min
ADOConnection.CommandTimeout := 5 * 60; // 5 min
ADOConnection.ConnectionString := <my connection string, one for each store>;
ADOConnection.ConnectionTimeout := 5;
ADOConnection.LoginPrompt := False;
ADOConnection.CursorLocation := clUseClient;
ADOConnection.KeepConnection := False;
ADOConnection.Attributes := [xaCommitRetaining,xaAbortRetaining];
ADOConnection.Connected := True;
Result := True;
Except
ADOStoreConnection.Connected := False; <- When the problem starts, this is where I go in each thread
End;
End;
.
- References:
- ADOConnection fail after a while in TThread
- From: Mikael Lenfors
- ADOConnection fail after a while in TThread
- Prev by Date: Re: Bulk Insert - Help
- Next by Date: ADO Copy Table to new MDB Help!
- Previous by thread: Re: ADOConnection fail after a while in TThread
- Next by thread: Re: ADOConnection fail after a while in TThread
- Index(es):
Relevant Pages
|
|