Two threads executing the same function

4

I have a situation where in Delphi I have two threads that execute a function that pings a Firebird database.

I use this to sync data from my POS (sending and receiving).

But as they run at the same time sometimes a dead lock occurs in the DB.

-

My role:

function TTPingRede(const IP: AnsiString; TimeOut: Integer = 250) : Boolean;
var
       DW: DWORD;
   Handle: THandle;
   InAddr: IPAddr;
      Arq: TextFile;
      Rep: array[1..128] of byte;'
begin
    try
       DW     := 99;
       Result := False;
       Handle := IcmpCreateFile;
if (Handle <> INVALID_HANDLE_VALUE) then
       begin
try
              TranslateStringToTInAddr(PansiChar(IP), InAddr);
              DW := IcmpSendEcho(Handle, InAddr, nil, 0, nil, @rep, 128, TimeOut);
              Result := (DW <> 0);
              vStatusREDE_ATIVA := Result;
           except
               DW     := 0;
               Result := False;
           end;
try
              Dm.qStatusRede.Close;
              Dm.qStatusRede.SQL.Clear;
              Dm.qStatusRede.SQL.Add('UPDATE TBSINCRO001');
              Dm.qStatusRede.SQL.Add('SET');
              Dm.qStatusRede.SQL.Add('   STATUS_REDE_SERVIDOR = :P0');
              Dm.qStatusRede.Params[0].AsInteger := DW;
              Dm.qStatusRede.Prepare;
              Dm.qStatusRede.ExecSQL;
if Dm.Tr_StatusRede.InTransaction then Dm.Tr_StatusRede.Commit;
           except
               on E: Exception do
               begin
                   Dm.Tr_StatusRede.Rollback;
                   GerarLogErroSincronizar('1:FUNC ENV|'+E.Message);
               end;
           end;
       end;
    finally
IcmpCloseHandle(Handle);
    end;
end;

I would like to know an implementation that I can ping the bank without deadlock.

    
asked by anonymous 11.09.2015 / 14:18

2 answers

1

Problems with competition usually have a common cause: shared objects

In this case, it appears that the shared object Dm is used by the two threads . This means that the two threads are sending and receiving data on the same connection. Think of it as two users typing SQL commands on the same keyboard, one interferes with the other without even committing.

Somehow, you need to ensure that each thread runs on a separate connection. The simplest way to do this is to create Dm1 and Dm2 and assign each to a thread . A more elaborate way would be to create a connection within the thread scope. Think of it as two users accessing the same database from different terminals, so with separate sessions and one does not interfere with the other until you commit the transaction.

    
13.11.2015 / 07:58
0

The TCP / IP stack organizes sessions between servers and clients through the process PID at each end of the connection. Since two threads are part of the same process, I believe they are sharing a session with the DBMS server, which causes DeadLock. Try creating two separate processes, one for each request to the server.

    
13.09.2015 / 03:02