Database Fail Over
#1
Posted 18 December 2012 - 08:53 AM
#2
Posted 19 December 2012 - 08:55 AM
DigitalAlien, on 18 December 2012 - 08:53 AM, said:
#3
Posted 19 December 2012 - 09:17 AM
#4
Posted 19 December 2012 - 10:27 AM
So, I am not familiar with HAProxy and how it could help me, I will however look into it.
The thing is, we already have servers with stuff on them, I am not in the networking department so load balancing is basically up to them. That being said, our old framework had DB fail over in the code and I have been tasked with doing that same thing in Yii. I need to know how to best approach it from that perspective, but also can recommend better ways if they are out there.
#5
Posted 19 December 2012 - 10:55 AM
#6
Posted 19 December 2012 - 10:58 AM
In the meantime, does anyone have any suggestions for doing this in Yii?
#7
Posted 19 December 2012 - 11:00 AM
DigitalAlien, on 19 December 2012 - 10:58 AM, said:
Yes. As I wrote above: Extend CDbConnection so you can specify multiple connection params. I remember somebody implementing split read/write like that. Unfortunately I cannot find that solution again ...
#8
Posted 19 December 2012 - 11:17 AM
So, if I took the route of extending CDbConnection, I am not sure exactly what methods I would change. I wouldn't know where to begin honestly. Where in CDbConnection does it get the config variable and connect to the DB? I assume that is where I would say "test connection" if it can't connect try the other.
#9
Posted 24 December 2012 - 08:16 AM
class DaDbConnection extends CDbConnection{ public $dbConnectTries = 6; public $numDatabases = 3; private $_tries =0; private $_db = 1; /* * Extends CDbConnection open() method * Tries to connect to database connections setup in config/main.php up to * the value of $dbConnectionTries or a connection is successful * @throws CException If it can not connect to any DBs */ protected function open() { try{ //try to connect to the default DB parent::open(); }catch(Exception $e){ if($this->_tries < $this->dbConnectTries){ //If there aren't anymore DBs to try we must start over from the first if($this->_db >= $this->numDatabases){ $tryDb = 'db'; $this->_db = 0; }else{ $tryDb = 'db'.$this->_db; } $this->_db++; $this->_tries++; $this->connectionString = Yii::app()->$tryDb->connectionString; $this->username = Yii::app()->$tryDb->username; $this->password = Yii::app()->$tryDb->password; $this->open(); }else{ throw new CDbException('Could Not Connect to a DB.'); } } }
#10
Posted 24 December 2012 - 01:35 PM
#11
Posted 26 December 2012 - 08:58 AM
Thanks!
#12
Posted 14 February 2013 - 04:17 PM
thanks