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.