CDbConnection different db connections for reading

Hi everybody!

There is a problem: the first server - r/w db, the second one - only for reading

There are lots of code with such syntax:

Yii::app()->dbreader->create… -> send read queries exclusively on the second server.

Have an idea how to balance read-requests between 2 servers, please criticize:

In main.php, db section for example:


'dbreader' => [

            'class' => 'CDbConnectionReader',

            'connectionString' => 'mysql:host=thefirstserver;dbname=db1',

            'emulatePrepare' => true,

            'username' => 'userremote',

            'password' => 'passremote',

            'charset' => 'utf8',

            'slaves' => [

                [

                    'connectionString' => 'mysql:host=thesecondserver;dbname=db1',

                    'username' => 'userremote',

                    'password' => 'passremote',

                ],

            ],

        ],



and in CDbConnectionClass:


class CDbConnectionReader extends CDbConnection

{

    public $slaves = [];


    protected function open()

    {

        $connectionOptions = $this->getRandomConnectionOptions();

        $this->connectionString = $connectionOptions['connectionString'];

        $this->username = $connectionOptions['username'];

        $this->password = $connectionOptions['password'];

        

        parent::open();

    }

    

    protected function getRandomConnectionOptions()

    {

        $options = [

            ['connectionString' => $this->connectionString, 'username' => $this->username, 'password' => $this->password,],

        ];

        if (is_array($this->slaves) && count($this->slaves)) {

            

            $options = CMap::mergeArray($options, $this->slaves);

        }

        

        $i = time() % count($options);

        return isset($options[$i]) ? $options[$i] : $options[0];

    }

}




something like that! what do you think about that solution?

[color="#556B2F"]/* Moved from "2.0" to "1.1.x" */[/color]