Page 1 of 1

unidac - mysql - create run time in a thread

Posted: Thu 24 May 2018 13:11
by roozgar
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

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

Posted: Thu 24 May 2018 13:43
by ViktorV
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.

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

Posted: Thu 24 May 2018 14:28
by roozgar
you mean i must create both connection and query in the separated anonymous thread?

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

Posted: Thu 24 May 2018 14:42
by ViktorV
Yes, you are right.

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

Posted: Thu 24 May 2018 15:18
by roozgar
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?!

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

Posted: Fri 25 May 2018 12:45
by ViktorV
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.

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

Posted: Sat 02 Jun 2018 07:13
by roozgar
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 ?

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

Posted: Mon 04 Jun 2018 10:50
by ViktorV
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.