summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sturgeon <email@philsturgeon.co.uk>2011-11-29 22:47:14 +0100
committerPhil Sturgeon <email@philsturgeon.co.uk>2011-11-29 22:47:14 +0100
commitff30be1c782a853b8c58f520214ac5079f273c42 (patch)
tree89281eeda4aa2eede95d64cd0adf7bcbbcd91d79
parent0bb866e6e1c55a536a3029eaaf1913e46512a51a (diff)
parent292a0f661baedaa0a2bd61795e564c4195f4b0b8 (diff)
Merge pull request #719 from fhjbalfoort/develop
DB_driver failover if a connection cannot be established to the main connection
-rw-r--r--application/config/database.php3
-rw-r--r--system/database/DB_driver.php37
-rw-r--r--user_guide_src/source/changelog.rst1
-rw-r--r--user_guide_src/source/database/configuration.rst42
4 files changed, 77 insertions, 6 deletions
diff --git a/application/config/database.php b/application/config/database.php
index 28b792f75..58eec4b30 100644
--- a/application/config/database.php
+++ b/application/config/database.php
@@ -62,6 +62,7 @@
| ['autoinit'] Whether or not to automatically initialize the database.
| ['stricton'] TRUE/FALSE - forces 'Strict Mode' connections
| - good for ensuring strict SQL while developing
+| ['failover'] array - A array with 0 or more data for connections if the main should fail.
|
| The $active_group variable lets you choose which connection group to
| make active. By default there is only one group (the 'default' group).
@@ -88,7 +89,7 @@ $db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
-
+$db['default']['failover'] = array();
/* End of file database.php */
/* Location: ./application/config/database.php */ \ No newline at end of file
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;
}
// ----------------------------------------------------------------
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 979755c06..5c7fd8d59 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -97,6 +97,7 @@ Release Date: Not Released
$this->db->like() in the :doc:`Database
Driver <database/active_record>`.
- Added $this->db->insert_batch() support to the OCI8 (Oracle) driver.
+ - Added failover if the main connections in the config should fail
- Libraries
diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst
index 687f0d920..433c67152 100644
--- a/user_guide_src/source/database/configuration.rst
+++ b/user_guide_src/source/database/configuration.rst
@@ -28,6 +28,48 @@ prototype::
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
+You can also specify failovers for the situation when the main connection cannot connect for some reason.
+These failovers can be specified by setting the failover for a connection like this::
+
+ $db['default']['failover'] = array(
+ array(
+ 'hostname' => 'localhost1',
+ 'username' => '',
+ 'password' => '',
+ 'database' => '',
+ 'dbdriver' => 'mysql',
+ 'dbprefix' => '',
+ 'pconnect' => TRUE,
+ 'db_debug' => TRUE,
+ 'cache_on' => FALSE,
+ 'cachedir' => '',
+ 'char_set' => 'utf8',
+ 'dbcollat' => 'utf8_general_ci',
+ 'swap_pre' => '',
+ 'autoinit' => TRUE,
+ 'stricton' => FALSE
+ ),
+ array(
+ 'hostname' => 'localhost2',
+ 'username' => '',
+ 'password' => '',
+ 'database' => '',
+ 'dbdriver' => 'mysql',
+ 'dbprefix' => '',
+ 'pconnect' => TRUE,
+ 'db_debug' => TRUE,
+ 'cache_on' => FALSE,
+ 'cachedir' => '',
+ 'char_set' => 'utf8',
+ 'dbcollat' => 'utf8_general_ci',
+ 'swap_pre' => '',
+ 'autoinit' => TRUE,
+ 'stricton' => FALSE
+ )
+ );
+
+You can specify as many failovers as you like.
+
The reason we use a multi-dimensional array rather than a more simple
one is to permit you to optionally store multiple sets of connection
values. If, for example, you run multiple environments (development,