Back to results for “Applications”
PHP › PDO & Databases

How should an application handle database connection failures gracefully, including implementing retry logic and connection health checks for a reliable production system?

Advanced

Answer

A robust application should catch connection related exceptions specifically, potentially implementing a limited number of automatic retry attempts with a brief delay for transient connection issues, such as a momentary network blip, while distinguishing these from genuine, non recoverable errors that should fail immediately and be logged for investigation, and larger applications sometimes implement periodic health checks that proactively verify a database connection is still valid before relying on it for a critical operation, rather than only discovering a stale or dropped connection when an actual query unexpectedly fails.
function connectWithRetry($dsn, $username, $password, $maxAttempts = 3) {
    for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
        try {
            return new PDO($dsn, $username, $password);
        } catch (PDOException $e) {
            if ($attempt === $maxAttempts) throw $e;
            sleep(1);
        }
    }
}
Real-world example A production application implements a connection retry mechanism with a short delay between attempts, successfully recovering automatically from a brief, transient network interruption to its database server that would have otherwise caused a completely avoidable outage.

Common follow-ups: How many retry attempts and what delay strategy is generally considered reasonable for this kind of transient failure handling?;What monitoring should be in place to alert a team if connection failures become persistent rather than transient?