unidac - mysql - create run time in a thread

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
roozgar
Posts: 12
Joined: Thu 24 May 2018 13:05

unidac - mysql - create run time in a thread

Post by roozgar » Thu 24 May 2018 13:11

hello
i need to connect to mysql in a anonymous thread .it must support two separated query two connect to two table.i do this with delphi native firedac but i got a lot of error and crash
is it possible to do this with unidac?
is there any sample for create component in run time !!?
thank you

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: unidac - mysql - create run time in a thread

Post by ViktorV » Thu 24 May 2018 13:43

UniDAC is thread-safe, but its limitation is that you can't use the same connection (TUniConnection component) in several threads, so you have to use a separate connection in each thread.

roozgar
Posts: 12
Joined: Thu 24 May 2018 13:05

Re: unidac - mysql - create run time in a thread

Post by roozgar » Thu 24 May 2018 14:28

you mean i must create both connection and query in the separated anonymous thread?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: unidac - mysql - create run time in a thread

Post by ViktorV » Thu 24 May 2018 14:42

Yes, you are right.

roozgar
Posts: 12
Joined: Thu 24 May 2018 13:05

Re: unidac - mysql - create run time in a thread

Post by roozgar » Thu 24 May 2018 15:18

thank you so much for your fast answers
i wrote this:

Code: Select all

function readData():string;
var
  UniQuery2 : tuniquery;
  UniConnection2 : tUniConnection;
begin
  UniConnection2 := TUniConnection.Create(nil);
  UniConnection2.ProviderName:='Mysql';
  UniConnection2.Server := '127.0.0.1';
  UniConnection2.Username := 'root';
  UniConnection2.Password := '';
  UniConnection2.Database := 'bazmine';
  UniConnection2.Connected := true;

  UniQuery2 := tuniquery.Create(nil);
  UniQuery2.Connection := UniConnection2;
  UniQuery2.SQL.Text := 'select * from visits;';
  UniQuery2.Active := true;

  try
    UniQuery2.Open;
    UniQuery2.First;
    while not UniQuery2.Eof do begin
        form8.memo1.Lines.Add( UniQuery2.FieldByName('add_date').Value );
      UniQuery2.Next;
    end;
  finally
    UniConnection2.Free;
    UniQuery2.Free;
  end;

end;

procedure TForm8.Button2Click(Sender: TObject);
begin
  TThread.CreateAnonymousThread(procedure ()
  begin
        readData();
  end).Start;
end;

the connection error problem resolved.
but when i run 5-6 thread same time it make ui form lagy again
is thre any problem in my code?!

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: unidac - mysql - create run time in a thread

Post by ViktorV » Fri 25 May 2018 12:45

If you mean that when starting multiple threads, your form starts to slow down - this is caused by using the TMemo component on your form and this behavior does not depend on UniDAC architecture, but is related to the specificity of developing multi-threaded applications. To solve the task, you should read the IDE documentation or ask a question at the specialized forums.
If you mean something else, please write us in more detail.

roozgar
Posts: 12
Joined: Thu 24 May 2018 13:05

Re: unidac - mysql - create run time in a thread

Post by roozgar » Sat 02 Jun 2018 07:13

every thing work ok
thank you
but i faced a wired issue
after a while the mysql server crash due to "too many connections" error
i changed max connection to 3000 from 151 but just time of error increased :|

how can i fix this?

i also used this block for close connection after end of the job but dont helped

Code: Select all

              finally
                UniQueryRead.Close;
                UniQueryRead.Free;
                UniConnectionRead.Close;
                UniConnectionRead.Free;
                UniQueryWrite.Close;
                UniQueryWrite.Free;
                UniConnectionWrite.Close;
                UniConnectionWrite.Free;
              end;
how can i resolve this ?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: unidac - mysql - create run time in a thread

Post by ViktorV » Mon 04 Jun 2018 10:50

This error is returned by MySQL server: https://dev.mysql.com/doc/refman/8.0/en ... tions.html and it occurs when all the available connections are already used. You can use SHOW PROCESSLIST to search for the issue cause.
Note, you can only call the TUniConnection.Free method (TUniQuery.Free), because, in this case, the TUniConnection.Close (TUniQuery.Close) method is called automatically.

Post Reply