How to address "Server gone away" error

On a TMS XData REST Server on Windows using Mysql ,I am seeing this error.

conFD: TFDPhysMySQLConnection: . MySQL server has gone away [errno=2006, sqlstate=“HY000”]

It happens after some inactivity. I am guessing that 8 hours may have passed and Mysql has disconnected to save resources. Its the only task running that uses Mysql, so resources are not an issue.

  • I could increase this period, but its will still happen
  • I could keep executing some nonsensical query periodically

How are others handing this.

You need to ping the database at regular intervals to keep the connection alive.

Look up FireDAC.Comp.Client.TFDCustomConnection.Ping in the help, it has an example of how to do that.

I am not using TMS, but for MySQL/MariaDB/FireDAC projects I use something like this timer event handler on a long duration timer:

procedure TdmPrim.DoDBPing;
begin
    { TODO 1 -oKevin -cCode Marker : Don't do ping if we are debugging (it messes up logging and Wireshark) }
    if DebugHook = 0 then begin
        if DBConnectionApp.Connected then
            DBConnectionApp.Ping;
        LastDBPing := Now;
    end;
end;

DBConnectionApp is a standard FireDAC TFDConnection.

Pinging the connection resets the timeout timer and keeps the connection open.

1 Like

Thats a problem. 12.3 install didnt install any help files. I am waiting for a free moment to reinstall :slight_smile:

But found it on Doc wiki which confirms Kevins reply. 30s is a bit brutal though. What I also found is that AutoReconnect property is off by default.

Thanks, good point about debugging

Rohit,

Cary Jensen’s book “Delphi in depth: FireDAC” (ISBN-13: 978-1546391272) is a good reference for novice FireDAC developers. I bought my copy on Amazon.

It is a bit dated (way behind in FireDAC versions), and uses a deprecated database engine in the examples, but it will generally get you going in the right direction.

My guess is that AutoReconnect will try to recover your connection after you lose it. Ping will keep the connection open proactively.

You should probably use both.