Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Reconnect on lost DB connection with PDO_MYSQL #302

Open
finalJustize opened this issue Jan 22, 2018 · 2 comments
Open

Reconnect on lost DB connection with PDO_MYSQL #302

finalJustize opened this issue Jan 22, 2018 · 2 comments

Comments

@finalJustize
Copy link

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

  • use some Tablegateway connected via PDO_MYSQL in a long running loop
  • while the loop runs, go to the MySQL-Server and kill the connection manually
  • you will get "HY000 - 2006 - MySQL server has gone away" in Tablegateway / Adapter / db Driver.

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

  • when the DB connection is lost the DB managing objects are not able to connect again. An error is thrown: "HY000 - 2006 - MySQL server has gone away" in Tablegateway / Adapter / db Driver.
@monteiro
Copy link

monteiro commented Apr 20, 2018

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
Copy link
Member

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at laminas/laminas-db#56.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants