Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping connection alive - mysql

I have set up my delphi application that I created with a button named Connect. Once I click it it executes the following code:

begin
    someConnection.Connected:=true;
    somecomenziDataSet.Active:=true;
end;

Similarly I have a button for Disconnect purposes which does the same thing but with =false;

My problem is the database I'm connecting to is hosted on a shared hosting account and the mysql server has wait_timeout variable set to 60 seconds, interactive_timeout is set to 30 seconds. Naturally, this disconnects me if I don't use my app for 60 seconds.

Is there any way to keep that connection alive?

Hosting company won't change the setting so i'm stuck with it.

I'm using RAD Studio 10 Seattle, dbexpress components, TSQLConnection and my database is mysql

Please do let me know in a comment if I left any necessary info out, thanks!

like image 409
t1f Avatar asked Nov 15 '16 17:11

t1f


1 Answers

You can use a TTimer to periodically "ping" the database (e.g. interval of 10-20 seconds given a 60 sec timeout):

procedure TMyDataModule.ConnectionPingTimer(Sender: TObject);
begin
  if not MySQLConnection.InTransaction then
    MySQLConnection.Execute('DO 0', nil); // or "SELECT 1" or whatever is cheapest
end;
like image 167
kobik Avatar answered Sep 23 '22 13:09

kobik