From a8eb5567167fcf0bc9a2988cb3ceeca38ad8e4e5 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 8 Dec 2011 13:56:33 +0000
Subject: [PATCH] consent: Add support for setting the timeout when connecting
 to the database.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2998 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/consent/docs/consent.txt              |  3 +++
 .../consent/lib/Consent/Store/Database.php    | 23 ++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/modules/consent/docs/consent.txt b/modules/consent/docs/consent.txt
index 092843e3f..f156a10e4 100644
--- a/modules/consent/docs/consent.txt
+++ b/modules/consent/docs/consent.txt
@@ -98,6 +98,9 @@ The `consent:Database` backend storage has the following options
 :   Name of the table used for storing the consents. This option is optional
 and defaults to `consent`.
 
+`timeout`
+:   The number of seconds to wait for a connection to the database server. This option is optional. If unset, it uses the default from the database-driver.
+
 Example config using PostgreSQL database:
 
 	90 => array(
diff --git a/modules/consent/lib/Consent/Store/Database.php b/modules/consent/lib/Consent/Store/Database.php
index 5ec4a4658..ad69a9111 100644
--- a/modules/consent/lib/Consent/Store/Database.php
+++ b/modules/consent/lib/Consent/Store/Database.php
@@ -39,6 +39,13 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
      */
     private $_table;
 
+    /**
+     * The timeout of the database connection.
+     *
+     * @var int|NULL
+     */
+    private $_timeout = NULL;
+
     /**
      * Database handle.
      *
@@ -86,6 +93,15 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
             $this->_table = 'consent';
         }
 
+        if (isset($config['timeout'])) {
+            if (!is_int($config['timeout'])) {
+                throw new Exception(
+                    'consent:Database - \'timeout\' is supposed to be an integer.'
+                );
+            }
+            $this->_timeout = $config['timeout'];
+        }
+
         // @TODO Should be removed
         $db = $this->_getDB();
     }
@@ -447,9 +463,14 @@ class sspmod_consent_Consent_Store_Database extends sspmod_consent_Store
             return $this->_db;
         }
 
+        $driver_options = array();
+        if (isset($this->_timeout)) {
+            $driver_options[PDO::ATTR_TIMEOUT] = $this->_timeout;
+        }
+
         // @TODO Cleanup this section
         //try {
-        $this->_db = new PDO($this->_dsn, $this->_username, $this->_password);
+        $this->_db = new PDO($this->_dsn, $this->_username, $this->_password, $driver_options);
         // 		} catch (PDOException $e) {
         // 			SimpleSAML_Logger::error('consent:Database - Failed to connect to \'' .
         // 				$this->_dsn . '\': '. $e->getMessage());
-- 
GitLab