database reconnection

I'm developing the phpdaemon script using the Phalcon's ORM and models.

It initializes all DB objects in the init routines and then runs 'forever'. In the meanwhile the MySQL connection might lost getting the following error:

Uncaught PDOException (HY000): SQLSTATE[HY000]: General error: 2006 MySQL server has gone away.
0 [internal function]: PDOStatement->execute()
1 [internal function]: Phalcon\Db\Adapter\Pdo->executePrepared(Object(PDOStatement), Array, NULL)
... etc etc ...

The question is how to determine if there is no connection and how to reconnect it?

Thank you.


I think you can add a checking code in your class before executing it, something like:

private function _checkConnection()
    try {
        $this->db->execute('SELECT 1');
    } catch (\PDOException $e) {
        if (strpos($e->getMessage(), 'MySQL server has gone away') !== false) {

public function anotherMethod()

    //Check connection