Skip to content

Commit 5d581b6

Browse files
committed
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 <fhjbalfoort@gmail.com>
1 parent 0bb866e commit 5d581b6

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

application/config/database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
| ['autoinit'] Whether or not to automatically initialize the database.
6363
| ['stricton'] TRUE/FALSE - forces 'Strict Mode' connections
6464
| - good for ensuring strict SQL while developing
65+
| ['failover'] array - A array with 0 or more data for connections if the main should fail.
6566
|
6667
| The $active_group variable lets you choose which connection group to
6768
| make active. By default there is only one group (the 'default' group).
@@ -88,7 +89,7 @@
8889
$db['default']['swap_pre'] = '';
8990
$db['default']['autoinit'] = TRUE;
9091
$db['default']['stricton'] = FALSE;
91-
92+
$db['default']['failover'] = array();
9293

9394
/* End of file database.php */
9495
/* Location: ./application/config/database.php */

system/database/DB_driver.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,43 @@ function initialize()
126126
// Connect to the database and set the connection ID
127127
$this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
128128

129-
// No connection resource? Throw an error
129+
// No connection resource? Check if there is a failover else throw an error
130130
if ( ! $this->conn_id)
131131
{
132-
log_message('error', 'Unable to connect to the database');
132+
// Check if there is a failover set
133+
if ( ! empty($this->failover) && is_array($this->failover))
134+
{
135+
// Go over all the failovers
136+
foreach ($this->failover as $failover)
137+
{
138+
// Replace the current settings with those of the failover
139+
foreach ($failover as $key => $val)
140+
{
141+
$this->$key = $val;
142+
}
133143

134-
if ($this->db_debug)
144+
// Try to connect
145+
$this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
146+
147+
// If a connection is made break the foreach loop
148+
if ($this->conn_id)
149+
{
150+
break;
151+
}
152+
}
153+
}
154+
155+
// We still don't have a connection?
156+
if ( ! $this->conn_id)
135157
{
136-
$this->display_error('db_unable_to_connect');
158+
log_message('error', 'Unable to connect to the database');
159+
160+
if ($this->db_debug)
161+
{
162+
$this->display_error('db_unable_to_connect');
163+
}
164+
return FALSE;
137165
}
138-
return FALSE;
139166
}
140167

141168
// ----------------------------------------------------------------

0 commit comments

Comments
 (0)