Quantcast
Channel: DA-SOFT Technologies - Forum
Viewing all articles
Browse latest Browse all 1040

Subject: Safe thread opperation when using Indy - by: artik

$
0
0
Hello, Can I ask about suggestion resolving following problem?: My application is a TCPServer application build on Indy Components - exactly on TidTCPServer. With this application connect several clients (up to 50 clients) and every of them periodically (ones per 5 seconds) sends information how to modyfy one record (generally every clients modifies different record, rarely the same) in shared Firebirds table X. Assuming that each active connection realizes by TidTCPServer is doing in separate Thread (is a good assumption?) I try to protect against conflicts with others Threads (also outside of using of TidTCPServer: I mean main thread of UI and several others do something in the backgroud on data stored id Table X) is my code correct: On the Form I have beside others components [code]ADConnection1: TADConnection; ADEventAlerter1: TADEventAlerter; ADPhysIBDriverLink2: TADPhysIBDriverLink;[/code] and main part of code (interests me the most) are something like this: [code]type Pointer_Client = ^TClient; TClient = record user: string; AContext: TIdContext; end; //START DATABASE CONNECTION var connection_parameters: TStrings; with connection_parameters do begin connection_parameters := TStringList.Create; Add('Database=' + 'c:/our_database.fdb'); Add('User_Name=SYSDBA'); Add('Password=masterkey'); Add('Pooled=True'); ADManager.AddConnectionDef('DB', 'IB', connection_parameters); end; ADManager.Active := true; ADConnection1.ConnectionDefName := 'our_database'; //event connected with geting new message from tcp client procedure TForm9.Serwer_WysylanieExecute(AContext: TIdContext); var oConn: TADConnection; oQuery: TADQuery; otrans: TADTransaction; lopened: boolean; begin . . . oConn := TADConnection.Create(nil); oConn.ConnectionDefName := 'DB'; otrans := TADTransaction.Create(nil); with otrans do begin Options.AutoCommit := true; Options.Isolation := xiReadCommitted; Connection := oConn; end; oQuery := TADQuery.Create(nil); oQuery.Transaction := otrans try oQuery.Connection := oConn; lopened := FALSE; oQuery.SQL.Text := 'update tableX set Z=1 where Y=:who'; oQuery.ParamByName('who').AsString:=Pointer_Client(AContext.Data.data).user; repeat try otrans.StartTransaction; begin oQuery.ExecSQL; lopened := true; except on E: EADDBEngineException do if pos('deadlock', E.Message) = 0 then raise; end; until lopened; AContext.Connection.socket.writeln('somethigh'); end; [/code] Have I stored information about: [code] oConn: TADConnection; oQuery: TADQuery; otrans: TADTransaction; [/code] in a TClient to avoid reinitialization it during next invoking onexecute events (to increase speed of doing code)? I ask about the possible suggestions or places where I should look for more information. Regards, Artik

Viewing all articles
Browse latest Browse all 1040

Trending Articles