Is Delphi's TADOConnection thread-safe?
Asked Answered
H

2

10

I'm writing a Delphi 7 application which needs to access the same SQL Server database from many different threads simultaneously.

Can I use a single shared TADOConnection, or must each thread create their own?

Honk answered 6/8, 2009 at 22:17 Comment(0)
E
20

Blorgbeard, you must create, initialize and open a separate TAdoconnection instance for each of your threads.

ADO is a COM-based technology. It uses apartment-threaded objects ,don't forget to call CoInitialize(nil).

procedure TMyThread.Execute;
begin
   CoInitialize(nil);
   try
     try
       // create a connection here
     except
     end;
   finally
     CoUnInitialize;
   end;
end;
Einkorn answered 6/8, 2009 at 22:37 Comment(0)
G
10

No, it is not. ADO is a COM-based technology. It uses apartment-threaded objects, thus you cannot use ADO connections across thread boundaries. Each thread need its own connection.

Giaimo answered 6/8, 2009 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.