ho cercato di trovare una soluzione per lo stesso problema e ho trovato la risposta successiva:
class NPDO {
private $pdo;
private $params;
public function __construct() {
$this->params = func_get_args();
$this->init();
}
public function __call($name, array $args) {
return call_user_func_array(array($this->pdo, $name), $args);
}
// The ping() will try to reconnect once if connection lost.
public function ping() {
try {
$this->pdo->query('SELECT 1');
} catch (PDOException $e) {
$this->init(); // Don't catch exception here, so that re-connect fail will throw exception
}
return true;
}
private function init() {
$class = new ReflectionClass('PDO');
$this->pdo = $class->newInstanceArgs($this->params);
}
}
completa storia qui: https://terenceyim.wordpress.com/2009/01/09/adding-ping-function-to-pdo/
Qualcun altro stava pensando di utilizzare PDO: ATTR_CONNECTION_STATUS, ma ha capito che: "$db->getAttribute(PDO::ATTR_CONNECTION_STATUS)
continua a rispondere" Localhost tramite socket UNIX "anche dopo aver interrotto mysqld".
fonte
2015-02-23 22:50:57
Puoi mostrare il tuo tentativo? – Rogue
perché non usare 'mysqli_ping()', se è falso allora prova a riconnettere – user1978142
@ user1978142 come fa mysqli_ping() help quando usa PDO? – user12345