{"id":"e4d6fb91731c744a","repo":"laravel/framework","slug":"lost-connection-and-no-reconnector-available","errorCode":null,"errorMessage":"Lost connection and no reconnector available.","messagePattern":"Lost connection and no reconnector available\\.","errorType":"exception","errorClass":"LostConnectionException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Connection.php","lineNumber":1056,"sourceCode":"        }\n\n        throw $e;\n    }\n\n    /**\n     * Reconnect to the database.\n     *\n     * @return mixed|false\n     *\n     * @throws \\Illuminate\\Database\\LostConnectionException\n     */\n    public function reconnect()\n    {\n        if (is_callable($this->reconnector)) {\n            return call_user_func($this->reconnector, $this);\n        }\n\n        throw new LostConnectionException('Lost connection and no reconnector available.');\n    }\n\n    /**\n     * Reconnect to the database if a PDO connection is missing.\n     *\n     * @return void\n     */\n    public function reconnectIfMissingConnection()\n    {\n        if (is_null($this->pdo)) {\n            $this->reconnect();\n        }\n    }\n\n    /**\n     * Disconnect from the underlying PDO connection.\n     *\n     * @return void","sourceCodeStart":1038,"sourceCodeEnd":1074,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Connection.php#L1038-L1074","documentation":"Thrown by Connection::reconnect() when its $reconnector callable is not set. The framework normally injects a reconnector Closure through DatabaseManager::configure() (setReconnector()), which knows how to rebuild the PDO handle for a given connection name. Seeing this means the connection has no way to rebuild itself after the socket died.","triggerScenarios":"An operation (run -> tryAgainIfCausedByLostConnection -> reconnect, or reconnectIfMissingConnection) fires after a dropped PDO link, on a Connection that was not produced by DatabaseManager. Happens with hand-built Connection instances, Capsule connections without Manager::setAsGlobal()/bootEloquent(), or when setReconnector() was bypassed.","commonSituations":"Long-running daemons/workers using a raw Connection; a connection object serialized and then reused; MySQL 'server has gone away' on a connection that has no reconnector; unit tests spinning up ConnectionFactory directly.","solutions":["Obtain the connection from DB::connection()/DatabaseManager so the reconnector Closure is attached in configure().","If you keep a bare Connection, call $connection->setReconnector(fn (Connection $c) => $c->setPdo($pdoFactory())) before use.","For long-lived workers, increase server wait_timeout / use keepalive queries so the link isn't dropped, or call DB::reconnect() from the manager side.","Catch \\Illuminate\\Database\\LostConnectionException and rebuild the connection from the manager."],"exampleFix":"// before\n$conn = $factory->make($config);\n$conn->statement('select 1'); // fails after a drop with no reconnector\n\n// after\n$conn = DB::connection($name);\n$conn->setReconnector(fn (Connection $c) => $c->setPdo($factory->createConnector($config)->connect($config)));","handlingStrategy":"try-catch","validationCode":"if (! is_callable($reflection = (new \\ReflectionObject($connection))->getProperty('reconnector')->getValue($connection))) {\n    $connection->setReconnector(fn (\\Illuminate\\Database\\Connection $c) =>\n        $c->setPdo($factory->createConnector($config)->connect($config)));\n}","typeGuard":"function hasReconnector(\\Illuminate\\Database\\Connection $c): bool {\n    $rp = (new \\ReflectionClass(\\Illuminate\\Database\\Connection::class))->getProperty('reconnector');\n    return is_callable($rp->getValue($c));\n}","tryCatchPattern":"try {\n    $connection->reconnect();\n} catch (\\Illuminate\\Database\\LostConnectionException $e) {\n    // rebuild the PDO from the manager side instead\n    DB::purge($name);\n    $connection = DB::connection($name);\n}","preventionTips":["Obtain connections via DatabaseManager so setReconnector() runs in configure().","In long-lived workers, periodically call DB::connection($name)->reconnectIfMissingConnection().","Never store a Connection across processes/serialization without re-establishing it.","Pair bare Connection creation with an explicit setReconnector() call."],"tags":["network","connection","configuration"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}