db logger needs to reconnect, too
authorChristian Weiske <cweiske@cweiske.de>
Wed, 15 Aug 2012 20:01:09 +0000 (22:01 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 15 Aug 2012 20:01:09 +0000 (22:01 +0200)
src/callnotifier/Logger/CallDb.php

index e2cdc6fd37946daae4e2310217b572e99b5a47c0..0135fba3723b30e8961f1c9afd36d43ee6ecbb66 100644 (file)
@@ -9,6 +9,11 @@ namespace callnotifier;
  */
 class Logger_CallDb extends Logger_CallBase
 {
  */
 class Logger_CallDb extends Logger_CallBase
 {
+    protected $db;
+    protected $dsn;
+    protected $username;
+    protected $password;
+
     /**
      * Create new detailler object
      *
     /**
      * Create new detailler object
      *
@@ -18,42 +23,36 @@ class Logger_CallDb extends Logger_CallBase
      * @param string $password Database password
      */
     public function __construct(
      * @param string $password Database password
      */
     public function __construct(
-        $dsn, $username, $password,
-        $callTypes = 'i', $msns = array()
+        $dsn, $username, $password, $callTypes = 'i', $msns = array()
     ) {
         parent::__construct($callTypes, $msns);
     ) {
         parent::__construct($callTypes, $msns);
+
+        $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(
         $this->db = new \PDO(
-            $dsn, $username, $password,
+            $this->dsn, $this->username, $this->password,
             array(
                 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
             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
             )
         );
             )
         );
-        $this->stmt = $this->db->prepare(
-            'INSERT INTO finished ('
-            . '  call_start'
-            . ', call_end'
-            . ', call_type'
-            . ', call_from'
-            . ', call_from_name'
-            . ', call_from_location'
-            . ', call_to'
-            . ', call_to_name'
-            . ', call_to_location'
-            . ', call_length'
-            . ') VALUES ('
-            . '  :call_start'
-            . ', :call_end'
-            . ', :call_type'
-            . ', :call_from'
-            . ', :call_from_name'
-            . ', :call_from_location'
-            . ', :call_to'
-            . ', :call_to_name'
-            . ', :call_to_location'
-            . ', :call_length'
-            . ')'
-        );
     }
 
     public function log($type, $arData)
     }
 
     public function log($type, $arData)
@@ -72,7 +71,9 @@ class Logger_CallDb extends Logger_CallBase
 
         $this->addUnsetVars($call);
 
 
         $this->addUnsetVars($call);
 
-        $ret = $this->stmt->execute(
+        $this->connect();
+        $stmt = $this->prepareDbStatement();
+        $ret  = $stmt->execute(
             array(
                 'call_start'         => date('Y-m-d H:i:s', $call->start),
                 'call_end'           => date('Y-m-d H:i:s', $call->end),
             array(
                 'call_start'         => date('Y-m-d H:i:s', $call->start),
                 'call_end'           => date('Y-m-d H:i:s', $call->end),
@@ -89,11 +90,40 @@ class Logger_CallDb extends Logger_CallBase
         if ($ret === false) {
             throw new \Exception(
                 'Error logging call to database: '
         if ($ret === false) {
             throw new \Exception(
                 'Error logging call to database: '
-                . implode(' / ', $this->stmt->errorInfo())
+                . implode(' / ', $stmt->errorInfo())
             );
         }
     }
 
             );
         }
     }
 
+    protected function prepareDbStatement()
+    {
+        return $this->db->prepare(
+            'INSERT INTO finished ('
+            . '  call_start'
+            . ', call_end'
+            . ', call_type'
+            . ', call_from'
+            . ', call_from_name'
+            . ', call_from_location'
+            . ', call_to'
+            . ', call_to_name'
+            . ', call_to_location'
+            . ', call_length'
+            . ') VALUES ('
+            . '  :call_start'
+            . ', :call_end'
+            . ', :call_type'
+            . ', :call_from'
+            . ', :call_from_name'
+            . ', :call_from_location'
+            . ', :call_to'
+            . ', :call_to_name'
+            . ', :call_to_location'
+            . ', :call_length'
+            . ')'
+        );
+    }
+
 }
 
 ?>
 }
 
 ?>