catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

How can I reconnect if the connection has been closed? #743

Closed nikoskip closed 3 years ago

nikoskip commented 6 years ago

Hi,

I'm using Medoo with MySQL in a very long process (it can take up to 2 hours to complete). When the process has finished I'm getting a MySQL server has gone away (2006).

I would like to identify this and reconnect before executing any other query to the server.

Thanks.

jamesst20 commented 6 years ago

@catfan

Seems to be ignoring PRs and issues reported, but looks like the project is still maintened.

2 years ago I believe a fix for this was pushed https://github.com/catfan/Medoo/pull/513 altough it's true the PR code isn't perfect

onefreddy commented 3 years ago

hi i have the same problem. I do not want to change timeout of the mysql. I would like to see a setting in the library configuration that allows automatic reconnections. sample:

$database = new Medoo([
    'database_type' => 'mysql',
    'database_name' => '',
    'server' => 'localhost',
    'username' => '',
    'password' => '',
    'autoreconifgoneaway' => true,
]);
oriondevelops commented 3 years ago

I also have the same problem. I don't have access to change timeout of mysql. I guess something like @onefreddy suggested would help. I could not solve it completely trying other things.

catfan commented 3 years ago

It's recommend to use singleton pattern for accessing database. https://medoo.in/api/collaboration

$app->singleton(function ($name, $singletonObject) {
    if ($name === "database") {
        if ($singletonObject === null) {
            // do something if database is lose
            echo "Connection is lose";

            // or reconnect the database
            $database = new Medoo([
                'database_type' => 'mysql',
                ...
            ]);
        }
    }
});

If the connection is lose, you can reconnect the database there, or connect another database, or just output the error message to client without reconnection. All those ways are up to you.