Lost connection to MySQL (over internet)

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
xxxFALKORxxx
Posts: 4
Joined: Fri 20 Jul 2018 17:42

Lost connection to MySQL (over internet)

Post by xxxFALKORxxx » Fri 12 Jun 2020 16:43

ive been reading all the post related to this problem and nothing seems to work.

i have a Delphi 7 app using MyDac components, on a local network everything works perfect but we need to move the MySQL database to a hosting and access it over internet, we have lots "loss of connection" problems

this we are done so far:
set Tmyconnection.LocalFailOver = True
trying different values to TmyConnection.ConnectionTimeOut (from 15 to 1000)

create a procedure for the Tmyconnection.OnconnectionLost event...

Code: Select all

procedure TDM.DirdatosConnectionLost(Sender: TObject;
  Component: TComponent; ConnLostCause: TConnLostCause;
  var RetryMode: TRetryMode);
begin
     RetryMode := rmReconnectExecute;
end;
what we can't do...
we read about wait_timeout MySQL system variable and max_allowed_packet server variable but the provider does not allow us to change the values (also Max_allowed_packet is related to large amount of data and we are getting loss connection error even with 1 record querys

What can we try?

xxxFALKORxxx
Posts: 4
Joined: Fri 20 Jul 2018 17:42

Re: Lost connection to MySQL (over internet)

Post by xxxFALKORxxx » Sat 13 Jun 2020 21:09

none a single answer...

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

Re: Lost connection to MySQL (over internet)

Post by ViktorV » Tue 16 Jun 2020 13:36

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/Devar ... 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 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.
To solve your task you can use the TMyConnection.Ping method. This method is used to test the connection to MySQL server if the connection was disconnected, there will be an error and an attempt to reconnect to the server.
As well this method can be used in order for the server not to close a connection due to timeout connection activity. In this case, you can use TMyConnection.Ping as follows - call it from the timer and set the interval timer to a value less than wait_timeout of the server (so that the intervals between commands receiving by the server do not exceed wait_timeout).

If this does not help in solving the problem, please write about the problem in more detail.

TobiasHo
Posts: 30
Joined: Thu 11 Jul 2019 09:12

Re: Lost connection to MySQL (over internet)

Post by TobiasHo » Mon 23 May 2022 19:21

I just implemented the code as described. When the MySQL Connection is lost (due to network problems), the program tries to reconnect whicht works fine.
Unfortunately the program opens a windows informing the user that the connection was lost.
Is it possible to suppress this error message?
"Lost connectio to MySQL server during query"

TobiasHo
Posts: 30
Joined: Thu 11 Jul 2019 09:12

Re: Lost connection to MySQL (over internet)

Post by TobiasHo » Wed 01 Jun 2022 06:05

No one has a solution for me?

pavelpd
Devart Team
Posts: 109
Joined: Thu 06 Jan 2022 14:16

Re: Lost connection to MySQL (over internet)

Post by pavelpd » Tue 07 Jun 2022 11:38

Hey,
Thanks for your request.

The above code example demonstrates the recovery of a lost connection with an attempt to perform the last failed operation, and should not give a connection loss error message.
You can check this behavior in the Demo project supplied with MyDAC.
The project is on the path: "%DemoDir%\MyDAC for RAD Studio 11\Demos\Miscellaneous\FailOver\", where %DemoDir% - is the path to the demo project folder that was defined when you installed MyDAC.
Please describe the exact steps of your actions leading to the error you have specified.
Do you apply additional options other than those described in the example above?

Also please compose and send us an sample application demonstrating the specified behavior.
You can send all the needed samples via the e-support form: https://www.devart.com/company/contactform.html

Please, let us know if you have any questions.

Post Reply