diff options
author | Phil Sturgeon <email@philsturgeon.co.uk> | 2011-11-29 22:47:14 +0100 |
---|---|---|
committer | Phil Sturgeon <email@philsturgeon.co.uk> | 2011-11-29 22:47:14 +0100 |
commit | ff30be1c782a853b8c58f520214ac5079f273c42 (patch) | |
tree | 89281eeda4aa2eede95d64cd0adf7bcbbcd91d79 /system/database | |
parent | 0bb866e6e1c55a536a3029eaaf1913e46512a51a (diff) | |
parent | 292a0f661baedaa0a2bd61795e564c4195f4b0b8 (diff) |
Merge pull request #719 from fhjbalfoort/develop
DB_driver failover if a connection cannot be established to the main connection
Diffstat (limited to 'system/database')
-rw-r--r-- | system/database/DB_driver.php | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index cc40ba48a..c2d57a833 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -126,16 +126,43 @@ class CI_DB_driver { // Connect to the database and set the connection ID $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - // No connection resource? Throw an error + // No connection resource? Check if there is a failover else throw an error if ( ! $this->conn_id) { - log_message('error', 'Unable to connect to the database'); + // Check if there is a failover set + if ( ! empty($this->failover) && is_array($this->failover)) + { + // Go over all the failovers + foreach ($this->failover as $failover) + { + // Replace the current settings with those of the failover + foreach ($failover as $key => $val) + { + $this->$key = $val; + } - if ($this->db_debug) + // Try to connect + $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); + + // If a connection is made break the foreach loop + if ($this->conn_id) + { + break; + } + } + } + + // We still don't have a connection? + if ( ! $this->conn_id) { - $this->display_error('db_unable_to_connect'); + log_message('error', 'Unable to connect to the database'); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_connect'); + } + return FALSE; } - return FALSE; } // ---------------------------------------------------------------- |