zendframework / zend-db

Db component from Zend Framework
BSD 3-Clause "New" or "Revised" License
101 stars 122 forks source link

Reconnect on lost DB connection with PDO_MYSQL #302

Open finalJustize opened 6 years ago

finalJustize commented 6 years ago

I am using php for long running scripts as a RabbitMq message comsumer. Messages can have DB access implemented as ZF3 / zend-db Tablegateway. The DB connection used is PDO_MYSQL. In case DB connection is killed (MYSQL_CONNECTION_TIMEOUT or other), AbstractTableGateway throws an error (HY000 - 2006 - MySQL server has gone away) when trying to execute a statement against the closed connection.

I expect that Tablegateway / Adapter / db Driver is smart enough to handle lost connection and reconnect or at least handle "unconnected" state and connect (again).

Code to reproduce the issue

Expected results

when the DB connection is lost the DB managing objects should be able to connect again to the DB again as they did initially.

Actual results

monteiro commented 6 years ago

I had the same prob. I had to catch the PDOException and do manually:

$this->adapter->getDriver()->getConnection()->disconnect();

so next time the worker tries to consume a message it will create a new connection.

In doctrine/dbal they've implemented a pingable connection to verify the connection: Connection.php Maybe it makes sense for this case.

michalbundyra commented 4 years ago

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at https://github.com/laminas/laminas-db/issues/56.