From 1886734503b6e65cb5c07d5b7edaca28cb86d803 Mon Sep 17 00:00:00 2001 From: Roman Seidl Date: Tue, 14 Apr 2015 22:22:36 +0200 Subject: [PATCH] add a ping() function check if the connection is alive, else invalidate it and try to reconnect --- wire/core/WireDatabasePDO.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/wire/core/WireDatabasePDO.php b/wire/core/WireDatabasePDO.php index 28b2ae28..270c7bf5 100644 --- a/wire/core/WireDatabasePDO.php +++ b/wire/core/WireDatabasePDO.php @@ -319,6 +319,21 @@ public function __get($key) { public function closeConnection() { $this->pdo = null; } + + /** + * Tries to query the db and if it fails tries to reconnect + * @return boolean + */ + public function ping() { + try { + $this->pdo->query('SELECT 1'); + } catch (PDOException $e) { + //force reconnect + $this->closeConnection(); + $this->pdo(); + } + return true; + } /** * Retrieve new instance of WireDatabaseBackups ready to use with this connection @@ -346,3 +361,4 @@ public function backups() { } } +