laracoft's avatar

General error: 2006 MySQL server has gone away

This often happens with daemons (long running processes) as the SQL connection might close due to inactivity.

I'm curious how queue:work overcomes it, but haven't been able to track down that "magic" code. Or is it because it queries the DB so often that it doesn't close? Anyone knows? Thank you.

0 likes
1 reply
laracoft's avatar
laracoft
OP
Best Answer
Level 27

I used this to deal with the problem, if there is a better way, please let me know. Thank you.

// v1
try {
    ...
} catch (\Exception $e) {
    if (Str::of($e->getMessage())->contains('MySQL server has gone away')) {
        DB::reconnect();
        ...
}

// v2
use Illuminate\Database\DetectsLostConnections;
...
try {
    ...
} catch (\Exception $e) {
    if ($this->causedByLostConnection($e)) {
        DB::reconnect();
        ...
}
1 like

Please or to participate in this conversation.