Re: ADOConnection fail after a while in TThread



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;



.



Relevant Pages

  • Re: C++ support for ADO
    ... TESTHR(pConn.CreateInstance(__uuidof(Connection))); ... In answer to your question I'm interested in ADO not the ADO.net. ... data classes for working with ADO/OLE DB. ... You can also use the OLE SDK. ...
    (microsoft.public.data.ado)
  • Re: ODBC/OLE DB Connection Pool
    ... > connection be kept open for the application as this will serialize all ... threads ONCE they are returned to the pool. ... > Tips for ADO Users ... > The ADO Connection object implicitly uses IDataInitialize. ...
    (microsoft.public.data.ado)
  • Re: ODBC/OLE DB Connection Pool
    ... > connection be kept open for the application as this will serialize all ... threads ONCE they are returned to the pool. ... > Tips for ADO Users ... > The ADO Connection object implicitly uses IDataInitialize. ...
    (microsoft.public.inetserver.asp.db)
  • Re: ODBC/OLE DB Connection Pool
    ... > connection be kept open for the application as this will serialize all ... threads ONCE they are returned to the pool. ... > Tips for ADO Users ... > The ADO Connection object implicitly uses IDataInitialize. ...
    (microsoft.public.data.oledb)
  • Re: How to make the app run?????
    ... throwing an error if the connection never was opened, ... How can i tell if i'm using ADO as in VB or .Net ADO ?? ... First of all, it was not shown to my customer, I took it to a very nice ... How do you get in "installed" on client computer? ...
    (microsoft.public.dotnet.general)