laravel/framework · error · LostConnectionException

Lost connection and no reconnector available.

Error message

Lost connection and no reconnector available.

What it means

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.

Source

Thrown at src/Illuminate/Database/Connection.php:1056

        }

        throw $e;
    }

    /**
     * Reconnect to the database.
     *
     * @return mixed|false
     *
     * @throws \Illuminate\Database\LostConnectionException
     */
    public function reconnect()
    {
        if (is_callable($this->reconnector)) {
            return call_user_func($this->reconnector, $this);
        }

        throw new LostConnectionException('Lost connection and no reconnector available.');
    }

    /**
     * Reconnect to the database if a PDO connection is missing.
     *
     * @return void
     */
    public function reconnectIfMissingConnection()
    {
        if (is_null($this->pdo)) {
            $this->reconnect();
        }
    }

    /**
     * Disconnect from the underlying PDO connection.
     *
     * @return void

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Obtain the connection from DB::connection()/DatabaseManager so the reconnector Closure is attached in configure().
  2. If you keep a bare Connection, call $connection->setReconnector(fn (Connection $c) => $c->setPdo($pdoFactory())) before use.
  3. 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.
  4. Catch \Illuminate\Database\LostConnectionException and rebuild the connection from the manager.

Example fix

// before
$conn = $factory->make($config);
$conn->statement('select 1'); // fails after a drop with no reconnector

// after
$conn = DB::connection($name);
$conn->setReconnector(fn (Connection $c) => $c->setPdo($factory->createConnector($config)->connect($config)));
Defensive patterns

Strategy: try-catch

Validate before calling

if (! is_callable($reflection = (new \ReflectionObject($connection))->getProperty('reconnector')->getValue($connection))) {
    $connection->setReconnector(fn (\Illuminate\Database\Connection $c) =>
        $c->setPdo($factory->createConnector($config)->connect($config)));
}

Type guard

function hasReconnector(\Illuminate\Database\Connection $c): bool {
    $rp = (new \ReflectionClass(\Illuminate\Database\Connection::class))->getProperty('reconnector');
    return is_callable($rp->getValue($c));
}

Try / catch

try {
    $connection->reconnect();
} catch (\Illuminate\Database\LostConnectionException $e) {
    // rebuild the PDO from the manager side instead
    DB::purge($name);
    $connection = DB::connection($name);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/e4d6fb91731c744a.json. Report an issue: GitHub.