dmlogic / pdo-retry-wrapper
PDO wrapper to gracefully handle dropped connections
Installs: 3 672
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.4|^8.0
- illuminate/database: ^8.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1
- phpstan/phpstan: ^0.12.99
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.3
README
I wish I didn't have to make this. But RDS under AWS is not nearly as available as the metrics claim.
This is inspired by the dropped connection retry mechanisms built into illuminate/database. I want the same here but at a much lower level for PDO.
Provides this functionality:
- Retry queries that failed due to connection issues up to a maximum number of attempts
- Define a callback to run if the limit is reached
- Combines
$pdo->prepare($sql)
and$pdo->execute($bindings)
into a single$connection->runQuery($sql, $bindings)
call
Usage
// Connector is a Closure for simpler reconnects $pdoConnector = function() { return new PDO( 'dsnString', 'username', 'password', [ PDO::OPTION_NAME => VALUE, ... ] ); }; // Optional exception callback can be anything at all // It's a handy place to centralise the error handling // logic if you don't want queries inside try/catch $callback = function(ConnectionException $exception) { $exception->getAttempts(); $exception->getOriginalException(); $exception->getQuery(); $exception->getBindings(); } // Create the wrapper $dbConnection = new Connection($pdoConnector, $callback); // Generate a PDOStatement with results try { $query = $dbConnection->runQuery('select * from users where id = ?', [123]); $user = $query->fetch(PDO::FETCH_OBJ); } catch(ConnectionException $e) { // We know we failed on connection // $callback has been invoked at this stage } catch(Exception $e) { // Something else went down }