Check for connection and keep alive

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
classic12
Posts: 21
Joined: Mon 20 Apr 2009 09:48

Check for connection and keep alive

Post by classic12 » Mon 16 Apr 2012 20:39

Delphi XE2 update 4 latest MYdac.

I am using the MYConnection in a Indy HTTP server app.

I run a query on the OnCommandGet . ie when a request comes in.

If the connection has dropped we get an error back stating no connection but I need to ensure that if the connection has dropped it is re-connected and then run my code.

Should I set a timer to poll a table every 30 mins.
Do a reconnect on the disconnected event ??

WHat is the correct method ( and code) please.

Cheers

SteveW

AndreyZ

Post by AndreyZ » Tue 17 Apr 2012 11:30

Hello,

To keep connection alive, you can ping MySQL server time after time. To ping MySQL Server, you can use the TMyConnection.Ping method that allows avoiding automatic disconnection of the client by the server.
To resume lost connection, you should use the TMyConnection.OnConnectionLost event handler. The OnConnectionLost event handler is used to process fatal errors and perform failover. To make the OnConnectionLost event handler work, you should set the TMyConnection.Options.LocalFailover property to True. Note that to use the OnConnectionLost event handler, you should add the MemData unit to the USES clause of your unit. Here is an example of using the OnConnectionLost event handler:

Code: Select all

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

procedure TMainForm.MyConnection1ConnectionLost(Sender: TObject;
  Component: TComponent; ConnLostCause: TConnLostCause;
  var RetryMode: TRetryMode);
begin
  RetryMode := rmReconnectExecute;
end;
In this case, if connection was lost, MyDAC will try to reconnect and reexecute the abortive operation. For more information, please read the MyDAC documentation.

classic12
Posts: 21
Joined: Mon 20 Apr 2009 09:48

Post by classic12 » Thu 19 Apr 2012 13:46

Thanks for the info. To clarify I need to set up a timer (say 1 hour) and use TMyConnection.Ping

If the connection fails it will re-open.

Is this correct ?

Cheers

SteveW

AndreyZ

Post by AndreyZ » Thu 19 Apr 2012 14:12

Yes, it is correct.

classic12
Posts: 21
Joined: Mon 20 Apr 2009 09:48

error popup

Post by classic12 » Fri 20 Apr 2012 17:55

I am getting a message box stating lost connection to MSQL. Socket error on read. WSAGetLastError return 10060(&2746).

I presume this is because I have pinged the mysql and there is no connection there until it re-connects.

How do I suppress this meesage ?


Cheers

SteveW

classic12
Posts: 21
Joined: Mon 20 Apr 2009 09:48

reply from delphi newsgroup

Post by classic12 » Mon 23 Apr 2012 21:52

I think you are talking about DevArt DAC. And if
you configured properly automatic connection recovery,
then DAC should not give you the exception, as it
should be handled internally and recover the connection.
But if the connection is impossible to open, then still
exception may be raised.

That is IMHO. The real answer you should get from
the vendor technical support.


Any ideas?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Wed 25 Apr 2012 09:37

Hello,

A successfull call of the Ping method is possible only when the connection is already set. If the connection is not set, than when calling this method, an attempt to connect to a server occurs, and if the server is out-of-reach, you get a relevant error message. To suppress this message, you should wrap the Ping method with a try...except block and process the error message

Post Reply