PHP code example of dmlogic / pdo-retry-wrapper
1. Go to this page and download the library: Download dmlogic/pdo-retry-wrapper library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dmlogic / pdo-retry-wrapper example snippets
// 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
}