From 064ad54ee0f56030effc6999ec7f63e867f784d2 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 14 Aug 2012 21:26:53 +0200 Subject: [PATCH] reconnect to the sql server before each sql query --- .../CallMonitor/Detailler/OpenGeoDb.php | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php b/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php index 46711b0..b50da13 100644 --- a/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php +++ b/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php @@ -15,6 +15,10 @@ namespace callnotifier; class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler { protected $db; + protected $dsn; + protected $username; + protected $password; + protected static $mobile = array( '0151' => 'Telekom', '0152' => 'Vodafone D2', @@ -45,12 +49,31 @@ class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler * @param string $password Database password */ public function __construct($dsn, $username, $password) + { + $this->dsn = $dsn; + $this->username = $username; + $this->password = $password; + //check if the credentials are correct + $this->connect(); + } + + /** + * Connect to the SQL server. + * SQL servers close the connection automatically after some hours, + * and since calls often don't come in every minute, we will have + * disconnects in between. + * Thus, we will reconnect on every location load. + * + * @return void + */ + protected function connect() { $this->db = new \PDO( - $dsn, $username, $password, + $this->dsn, $this->username, $this->password, array( \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', - \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_PERSISTENT => true ) ); } @@ -81,6 +104,7 @@ class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler //FIXME: what about international numbers? //area codes in germany can be 3 to 6 numbers + $this->connect(); $stm = $this->db->query( 'SELECT name FROM my_orte' . ' WHERE vorwahl = ' . $this->db->quote(substr($number, 0, 3)) -- 2.30.2