Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

When server has gone away a long time, will always throw "No PDO connection is set" #6

Closed
Moln opened this issue May 9, 2019 · 3 comments

Comments

@Moln
Copy link

Moln commented May 9, 2019

<?php

use LegoW\ReconnectingPDO\ReconnectingPDO;

include 'vendor/autoload.php';

$config = [
    'dsn' => 'mysql:host=127.0.0.1;dbname=test;charset=utf8mb4',
    'username' => 'root',
    'password' => '',
];

$pdo = new ReconnectingPDO($config['dsn'], $config['username'], $config['password']);

sleep(5);
// In sleep times, do `service mysql stop`

while (true) {
    try {
        $sth = $pdo->prepare('select 1');
        $sth->execute();
        $sth->fetchAll();
    } catch(\Exception $e) {
        // Throw ReconnectingPDOException: No PDO connection is set
        // And a PHP Notice:  Undefined property: LegoW\ReconnectingPDO\ReconnectingPDO::$connection
        echo $e; 
        echo "------------------\n";
    }

    sleep(5);
}

Expected result:

Reconnect, when server was start.

Actual result:

Throw ReconnectingPDOException: No PDO connection is set.

And a PHP Notice: Undefined property: LegoW\ReconnectingPDO\ReconnectingPDO::$connection

@adamturcsan
Copy link
Owner

Hey,
Actually this case is not tested/not targeted, as this lib is mainly for "Mysql server has gone away" type errors for long idling connections.
But it could be a valid use case, if there's the resource to implement it.

And of course #7 is a long overdue task to clarify the misunderstandings...

@Maxim-Mazurok
Copy link
Contributor

Yeah, I agree with @adamturcsan
@Moln can you give a valid use-case or real-world situation when such error occurs?
From my point of view, it's only possible in case if the server is MySQL server is down, which doesn't seem like a good thing in the first place. You probably want to set up monitoring with alerts on your MySQL server to prevent this. Also, you probably should inform your users that the DB is down.
Anyway, I would recommend making PR if you still want to implement this. But please, make this optional. Somebody may not have MySQL alerts in place and will rely on the server log, looking for errors.
Thanks!

@Moln
Copy link
Author

Moln commented Jul 16, 2019

Thanks guys!

At that time , I'm maintaining and upgrading my Mysql server.
This must stop for a while. Then my daemon program throw an exception ( No PDO connection is set ) and exit.
So I saw the ReconnectingPDO.php code. Then I know , this is just to work for long idling connections.

The last, I change to use doctrine/dbal, and using ping() and connect() instead. Because I read this issue doctrine/dbal#1454

@Moln Moln closed this as completed Jul 16, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants