5

Problem

We tell PDO to wrap each issue into exception. In some cases it generates a few warnings and only then throws exception.

Why does it do it?

Duplicates?

There were no correct answers on SO regarding it. Last question was PHP PDO Exception + Warning on MySQL Has Gone Away? but people just marked it as duplicate instead of carefully answer.

Accepted answer not answers why does it do it and when. So I researched and will answer.

Community
  • 1
  • 1
gaRex
  • 4,144
  • 25
  • 37

2 Answers2

3

It's because of PDO could use mysqlnd driver, which not respects any of PDO's "convert-issues-to-extensions" policy.

Just look at sources of mysqlnd driver.

We clearly see there direct calls to php_error_docref.

And one of examples, shown at previous question explained by these lines: https://github.com/php/php-src/blob/PHP-5.5.31/ext/mysqlnd/mysqlnd_wireprotocol.c#L35:L61

Community
  • 1
  • 1
gaRex
  • 4,144
  • 25
  • 37
0

Use set_error_handler() and restore_error_handler()

public function query($sql)
{
    $retries = 0;
    $result = null;

    while (true) {
        try {
            set_error_handler(function () {
            });
            $result = $this->pdo->query($sql);
            restore_error_handler();
            break;
        } catch (Exception $e) {
            restore_error_handler();
            if (++$retries < self::RECONNECT_ATTEMPT) {
                if (strpos($e->getMessage(), 'server has gone away') !== false) {
                    $this->connect();
                    $this->getLogger()->info('Reconnect database, reason: server has gone away');
                }
            } else {
                throw $e;
            }
        }
    }
    return $result;
}
Opalosolo
  • 252
  • 2
  • 7
  • It's obvious. The question was about "why" it happens. Not how to cure it. Anyway it will help fot those who don't know it. – gaRex Apr 07 '17 at 14:12