From ea6e466309887cef69f5685f97d17e6e6b335c2f Mon Sep 17 00:00:00 2001 From: Felix Balfoort Date: Tue, 29 Nov 2011 15:53:01 +0100 Subject: The DB_driver can now use failover databases if specified The DB_driver can now use failover databases if specified. If the main connection shouldn't connect for some reason the DB_driver will now try to connect to specified connections in the failover config. Example config: $db['default']['hostname'] = 'localhost'; $db['default']['username'] = ''; $db['default']['password'] = ''; $db['default']['database'] = ''; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; $db['default']['failover'] = array(); $db['default']['failover'][0]['hostname'] = 'localhost1'; $db['default']['failover'][0]['username'] = ''; $db['default']['failover'][0]['password'] = ''; $db['default']['failover'][0]['database'] = ''; $db['default']['failover'][0]['dbdriver'] = 'mysql'; $db['default']['failover'][0]['dbprefix'] = ''; $db['default']['failover'][0]['pconnect'] = TRUE; $db['default']['failover'][0]['db_debug'] = TRUE; $db['default']['failover'][0]['cache_on'] = FALSE; $db['default']['failover'][0]['cachedir'] = ''; $db['default']['failover'][0]['char_set'] = 'utf8'; $db['default']['failover'][0]['dbcollat'] = 'utf8_general_ci'; $db['default']['failover'][0]['swap_pre'] = ''; $db['default']['failover'][0]['autoinit'] = TRUE; $db['default']['failover'][0]['stricton'] = FALSE; $db['default']['failover'][0]['failover'] = array(); $db['default']['failover'][1]['hostname'] = 'localhost2'; $db['default']['failover'][1]['username'] = ''; $db['default']['failover'][1]['password'] = ''; $db['default']['failover'][1]['database'] = ''; $db['default']['failover'][1]['dbdriver'] = 'mysql'; $db['default']['failover'][1]['dbprefix'] = ''; $db['default']['failover'][1]['pconnect'] = TRUE; $db['default']['failover'][1]['db_debug'] = TRUE; $db['default']['failover'][1]['cache_on'] = FALSE; $db['default']['failover'][1]['cachedir'] = ''; $db['default']['failover'][1]['char_set'] = 'utf8'; $db['default']['failover'][1]['dbcollat'] = 'utf8_general_ci'; $db['default']['failover'][1]['swap_pre'] = ''; $db['default']['failover'][1]['autoinit'] = TRUE; $db['default']['failover'][1]['stricton'] = FALSE; $db['default']['failover'][1]['failover'] = array(); Signed-off-by: Felix Balfoort --- application/config/database.php | 19 +++++++++++++++++++ system/database/DB_driver.php | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/application/config/database.php b/application/config/database.php index 28b792f75..12ef7fed0 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,6 +89,24 @@ $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; +$db['default']['failover'] = array(); + +$db['default']['hostname'] = 'localhost'; +$db['default']['username'] = ''; +$db['default']['password'] = ''; +$db['default']['database'] = ''; +$db['default']['dbdriver'] = 'mysql'; +$db['default']['dbprefix'] = ''; +$db['default']['pconnect'] = TRUE; +$db['default']['db_debug'] = TRUE; +$db['default']['cache_on'] = FALSE; +$db['default']['cachedir'] = ''; +$db['default']['char_set'] = 'utf8'; +$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 */ 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; } // ---------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 5d581b6d664c6ef662bcb9572ca147ac17af52cb Mon Sep 17 00:00:00 2001 From: Felix Balfoort Date: Tue, 29 Nov 2011 15:53:01 +0100 Subject: The DB_driver can now use failover databases if specified The DB_driver can now use failover databases if specified. If the main connection shouldn't connect for some reason the DB_driver will now try to connect to specified connections in the failover config. Example config: $db['default']['hostname'] = 'localhost'; $db['default']['username'] = ''; $db['default']['password'] = ''; $db['default']['database'] = ''; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; $db['default']['failover'] = array(); $db['default']['failover'][0]['hostname'] = 'localhost1'; $db['default']['failover'][0]['username'] = ''; $db['default']['failover'][0]['password'] = ''; $db['default']['failover'][0]['database'] = ''; $db['default']['failover'][0]['dbdriver'] = 'mysql'; $db['default']['failover'][0]['dbprefix'] = ''; $db['default']['failover'][0]['pconnect'] = TRUE; $db['default']['failover'][0]['db_debug'] = TRUE; $db['default']['failover'][0]['cache_on'] = FALSE; $db['default']['failover'][0]['cachedir'] = ''; $db['default']['failover'][0]['char_set'] = 'utf8'; $db['default']['failover'][0]['dbcollat'] = 'utf8_general_ci'; $db['default']['failover'][0]['swap_pre'] = ''; $db['default']['failover'][0]['autoinit'] = TRUE; $db['default']['failover'][0]['stricton'] = FALSE; $db['default']['failover'][0]['failover'] = array(); $db['default']['failover'][1]['hostname'] = 'localhost2'; $db['default']['failover'][1]['username'] = ''; $db['default']['failover'][1]['password'] = ''; $db['default']['failover'][1]['database'] = ''; $db['default']['failover'][1]['dbdriver'] = 'mysql'; $db['default']['failover'][1]['dbprefix'] = ''; $db['default']['failover'][1]['pconnect'] = TRUE; $db['default']['failover'][1]['db_debug'] = TRUE; $db['default']['failover'][1]['cache_on'] = FALSE; $db['default']['failover'][1]['cachedir'] = ''; $db['default']['failover'][1]['char_set'] = 'utf8'; $db['default']['failover'][1]['dbcollat'] = 'utf8_general_ci'; $db['default']['failover'][1]['swap_pre'] = ''; $db['default']['failover'][1]['autoinit'] = TRUE; $db['default']['failover'][1]['stricton'] = FALSE; $db['default']['failover'][1]['failover'] = array(); Signed-off-by: Felix Balfoort --- application/config/database.php | 3 ++- system/database/DB_driver.php | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 34 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; } // ---------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 85fe96df4dd7b7e163fae2d7e0420e750559e65c Mon Sep 17 00:00:00 2001 From: Felix Balfoort Date: Tue, 29 Nov 2011 16:27:53 +0100 Subject: Updated change log and and database configuration to include failover --- user_guide_src/source/changelog.rst | 1 + user_guide_src/source/database/configuration.rst | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) 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 `. - 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..a6bfc8979 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -28,6 +28,28 @@ 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'][0]['hostname'] = 'localhost1'; + $db['default']['failover'][0]['username'] = ''; + $db['default']['failover'][0]['password'] = ''; + $db['default']['failover'][0]['database'] = ''; + $db['default']['failover'][0]['dbdriver'] = 'mysql'; + $db['default']['failover'][0]['dbprefix'] = ''; + $db['default']['failover'][0]['pconnect'] = TRUE; + $db['default']['failover'][0]['db_debug'] = TRUE; + $db['default']['failover'][0]['cache_on'] = FALSE; + $db['default']['failover'][0]['cachedir'] = ''; + $db['default']['failover'][0]['char_set'] = 'utf8'; + $db['default']['failover'][0]['dbcollat'] = 'utf8_general_ci'; + $db['default']['failover'][0]['swap_pre'] = ''; + $db['default']['failover'][0]['autoinit'] = TRUE; + $db['default']['failover'][0]['stricton'] = FALSE; + $db['default']['failover'][0]['failover'] = array(); + +You can specify as much 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, -- cgit v1.2.3-24-g4f1b From 68f5d49d838fb2822b0f41e9b270f57e386f3fe4 Mon Sep 17 00:00:00 2001 From: Felix Balfoort Date: Tue, 29 Nov 2011 17:03:23 +0100 Subject: changed much to many in the documentation --- user_guide_src/source/database/configuration.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst index a6bfc8979..60a2026e5 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -48,7 +48,7 @@ These failovers can be specified by setting the failover for a connection like t $db['default']['failover'][0]['stricton'] = FALSE; $db['default']['failover'][0]['failover'] = array(); -You can specify as much failovers as you like. +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 -- cgit v1.2.3-24-g4f1b From 292a0f661baedaa0a2bd61795e564c4195f4b0b8 Mon Sep 17 00:00:00 2001 From: Felix Balfoort Date: Tue, 29 Nov 2011 22:29:33 +0100 Subject: Changed the array structure in the userguide --- user_guide_src/source/database/configuration.rst | 52 ++++++++++++++++-------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst index 60a2026e5..433c67152 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -31,22 +31,42 @@ prototype:: 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'][0]['hostname'] = 'localhost1'; - $db['default']['failover'][0]['username'] = ''; - $db['default']['failover'][0]['password'] = ''; - $db['default']['failover'][0]['database'] = ''; - $db['default']['failover'][0]['dbdriver'] = 'mysql'; - $db['default']['failover'][0]['dbprefix'] = ''; - $db['default']['failover'][0]['pconnect'] = TRUE; - $db['default']['failover'][0]['db_debug'] = TRUE; - $db['default']['failover'][0]['cache_on'] = FALSE; - $db['default']['failover'][0]['cachedir'] = ''; - $db['default']['failover'][0]['char_set'] = 'utf8'; - $db['default']['failover'][0]['dbcollat'] = 'utf8_general_ci'; - $db['default']['failover'][0]['swap_pre'] = ''; - $db['default']['failover'][0]['autoinit'] = TRUE; - $db['default']['failover'][0]['stricton'] = FALSE; - $db['default']['failover'][0]['failover'] = array(); + $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. -- cgit v1.2.3-24-g4f1b