We have a console FreePascal application that works with a MySQL database. It connects to the remote server from a client-side machine. Reads and writes data. Everything works fine, stable due to Transaction handling, but if they don’t work from the database for a few minutes, it will disconnect and we have to reconnect.
I’ve already set up 2 things to reconnect faster:
mysql_options () -> MYSQL_OPT_RECONNECT = 1
mysql_options () -> MYSQL_OPT_CONNECT_TIMEOUT = 1
So it got a lot better, but not perfect. Continuous disconnection, reconnection. I thought this could be solved by keeping the MySQL connection alive.
Under Lazarus, there is TTimer, which is very easy to use. For example, it would be a good idea to send a mysql_ping() to the server every 10 minutes. Unfortunately, this is not so easy for console applications.
I read and looked at some documentation and downloaded some source code samples to understand how it works. If I understood correctly, this can be solved here with TFPTimer.
The following small program prints a dot every 1 second and the timer prints the time every 5 seconds. Its operation is pretty much understandable.
The problem is that I can't embed this in the console app because if I understand CheckSynchronize() should be started all the time and users are doing something, reading or writing data in one of the menu items. I can't put this CheckSynchronize() call everywhere.
How can this be solved? Or is there any other way to keep my MySQL connection alive?
I attach the console timer app to better understand the problem.
Thanks for the help!