Re: Is This Thread Safe?



Hi Mark,

I wouldn't share any data components. Create all your queries and
connection components in each thread's execution method so their local to
each thread and get disposed of when thread terminates.

I have a framework that uses TIdCmdTCPServer, and for each client connection
that processes or retrieves data I create a separate AdoQuery and
AdoConnection.

Also, using adoqST.Create(nil) will not work. The syntax should be: adoqST
:= TAdoQuery.Create(nil);

Here's the way I would write it, but I'm sure others will have an opinion as
well... ;)

// FLock is a TCriticalSection
Function TSomeForm.GetConnStr : String ;
Begin
Try
FLock.Acquire ;
Result := SomeConnectionString ;
Finally
FLock.Release ;
End;
End;

Procedure TSomeForm.SomeThreadExecute ;
Var
AQry : TAdoQuery ;
AConn: TAdoConnection ;
Begin
AQry := Nil ;
AConn := Nil ;
Try
CoInitialize(nil); // or CoInitializeEx(Nil,COINIT_APARTMENTTHREADED);
AQry := TAdoQuery.Create(Nil) ;
AConn := TAdoConnection.Create(nil) ;
AConn.ConnectionString := GetConnStr;
AQry.Connection := AdoConn ;
// --> Process your stuff here
Finally
If AQry <> Nil Then AQry.Free ;
If AConn <> Nil Then AConn.Free ;
CoUnInitialize(nil);
End;
End;

HTH,
-Steve-

"Mark Moss" <markmoss@xxxxxxxxxxxx> wrote in message
news:480cdd58$1@xxxxxxxxxxxxxxxxxxxxxxxxx
Ladies / Gentlemen


Could someone or several someones please take a look at the
following code and tell me if this is thread safe and if not why not and
how to correct it.

Thanks

Mark Moss


.