connect to mysql server over the internet

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

connect to mysql server over the internet

Post by roozgar » Thu 04 Apr 2019 21:21

hello
i need to connect to mysql server over the web
my code is like this and working good on localhost

Code: Select all

try
    try
        UniConnectionRead := TUniConnection.Create(nil);
        UniConnectionRead.ProviderName:='Mysql';
        UniConnectionRead.Server := globalDBIP;
        UniConnectionRead.Username := globalDBUn;
        UniConnectionRead.Password := globalDBPass;
        UniConnectionRead.Database := globalDBName;
        UniConnectionRead.AutoCommit := true;
        UniConnectionRead.SpecificOptions.Values['MySQL.UseUnicode'] := 'True';
        UniConnectionRead.Connected := true;

        UniQueryRead := tuniquery.Create(nil);
        UniQueryRead.Connection := UniConnectionRead;
	
	//code for read write to database
	
    Except
      Form1.ListBox1.Items.Add('Error:cant get atunomy page list from database!');
    end;
finally
    UniQueryRead.Free;
    UniConnectionRead.Free;
end;
but on the web for low connection speed or temporary disconnecting the code will crashed and application hanging
i have two question for a better experience :

1)how can do a reconnect or handle error to avoid crashes

2)i have some codes in my application thos connecting to database server separately,can i use one single connection for all 'tuniquery' ? i mean i have one connaction and handle its reconnect to server and 20-30 tuniquery use this connection to connect to server!? (now each tuniquery have its own TUniConnection )

Thank you

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

Re: connect to mysql server over the internet

Post by ViktorV » Fri 05 Apr 2019 08:57

1) To restore a lost connection you need to use the event handler TMyConnection.OnConnectionLost. To enable the OnConnectionLost event handler set the property TMyConnection.Options.LocalFailover to True. Please note to use the OnConnectionLost handler you need to add the MemData unit to the USES section of your unit. An example of using OnConnectionLost:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyConnection1.Options.LocalFailover := True;
  MyConnection1.Open;
end;

procedure TForm1.MyConnection1ConnectionLost(Sender: TObject;
Component: TComponent; ConnLostCause: TConnLostCause;
var RetryMode: TRetryMode);
begin
  RetryMode := rmReconnectExecute;
end;
In this case when connection is lost MyDAC will try to reconnect and restart the failed operation. Read more in MyDAC documentation: https://www.devart.com/mydac/docs/Deva ... onLost.htm
The OnConnectionLost event occurs only when the following conditions are fulfilled:
- a fatal error occurs;
- there are no opened transactions in a connection that are not ReadOnlyReadCommitted (if connection has at least one opened transaction, which is not ReadCommitedReadOnly, FailOver does not execute. All ReadCommitedReadOnly transaction are restored with FailOver operation);
- there are no opened and non-fetched datasets;
- there are no explicitly prepared datasets or SQLs.
Please make sure that none of the conditions above is violated.

Note that MyDAC does not automatically initiate checking connection to the server. Therefore, after successfully connecting to the server if the connection to the server disconnects the TMyConnection.Connected property will be set to True and the OnConnectionLost event will not be initiated until there is an attempt to connect to the server.

2) In a single-threaded application, there should not be a problem when using one global connection.
If you are using a multi-threaded application, then a separate connection (the TMyConnection component) per thread must be used.

You can get more information about performance improving when using our components at our website: https://www.devart.com/mydac/docs/index ... rmance.htm

Post Reply