Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 Master / Slave connections

Tags:

doctrine-orm

I'm looking at different ways of settings up Doctrine 2 to use master/slave connections usng MySQL. The set up would be so that there is one master database with multiple slaves. All SELECT statements should come from a random live slave and any UPDATE, INSERT, DELETE statements would always be delegated to the master connection.

Has anyone set up Doctine 2 in this way or have any ideas on how to approach it?

like image 768
Kevin Avatar asked Feb 07 '11 14:02

Kevin


1 Answers

Doctrine2 now has a MasterSlaveConnection in the \Doctrine\DBAL\Connections namespace.

EDIT: Dont read below unless this piece doesnt work

Dont need an overloader anymore, the dbal configs will take the slaves by itself. e.g.

connections:
            default:
                driver:   %database_driver%
                host:     %database_host%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                slaves:
                    slave1:
                        host:     %database_slave1%
                        dbname:   %database_name%
                        user:     %database_user%
                        password: %database_password%

If the above doesnt work, try this

Something simple, just put pipes (|) between each host

default:
                driver:   %database_driver%
                host:     %database_host%|%database_slave%|%database_slave2%
                port:     3306
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                wrapper_class: \Foo\Bar\Symfony\Doctrine\Connections\MasterSlave


<?php
namespace Foo\Bar\Symfony\Doctrine\Connections;
use \Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Connection,
        Doctrine\DBAL\Driver,
        Doctrine\DBAL\Configuration,
        Doctrine\Common\EventManager,
        Doctrine\DBAL\Event\ConnectionEventArgs,
        Doctrine\DBAL\Events,
        Doctrine\DBAL\Cache\QueryCacheProfile;

class MasterSlave extends MasterSlaveConnection
{
        public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
        {
                $tempParams = array(
                                'master' => array()
                                , 'slaves' => array()
                                , 'driver' => $params['driver']
                                );
                $hosts = explode('|', $params['host']);
                unset($params['host']);
                foreach($hosts as $num => $host)
                {
                        $params['host'] = $host;
                        if($num == 0)
                        {
                                $tempParams['master'] = $params;
                        }
                        else
                        {
                                $tempParams['slaves'][] = $params;
                        }
                }

                if(!isset($tempParams['master']['driver']))
                        $tempParams['master']['driver'] = "pdo_mysql";

                foreach($tempParams['slaves'] as $k => $slave)
                {
                        if(!isset($slave['driver']))
                                $tempParams['slaves'][$k]['driver'] = "pdo_mysql";
                }
                parent::__construct($tempParams, $driver, $config, $eventManager);
        }

        public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
        {
                try
                {
                        return parent::executeQuery($query, $params, $types, $qcp);
                }
                catch(\Exception $e)
                {
                        $logger = new \Uelib\Core\Logger();
                        $message = $e->getMessage() . "\nSql: " . $query . "\nParams: \n" . print_r($params, true);
                        $logger->log($message);
                        throw $e;
                }
        }
}
like image 119
Ascherer Avatar answered Sep 22 '22 06:09

Ascherer