We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Using PhalconPHP db interface within a PHP websocket server

I have a php script which is a websocket server. It has a main loop that runs forever, and theres where my problem comes -- I'm using phalcon models and database interface in it, so after ~12h of running i get the 'Mysql Has Gone Away' because the connection was alive for too long. What would be the efficient solution to overcome this issue, can i simply close and open a new connection each time a function executes in the websock server or is that a bad approach?

edited Jan '16

Following your answer on irc: Oppening and closing connexion is what happens everytime a user call a page via http, including ajax calls, it would not be slower than doing an ajax call that would manage http connexion + sql connexion



7.9k

you could ping mysql for every x hour/minutes or using try catch statement if mysql connection is closed create new connection.

If your only issue is persistency, pinging the server with a SELECT 1 statement at regular intervals (say 10 minutes) is the most efficient.