Permalink
Browse files

Add option to keep the slave in master/slave setups. You can call $co…

…nn->connectTo("slave") to get back to the slave connection
  • Loading branch information...
1 parent 4eefceb commit 4eaa44c057c228ea7a2fa9cb6cff0b9f94529528 @beberlei beberlei committed Jun 27, 2012
Showing with 16 additions and 2 deletions.
  1. +16 −2 lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php
@@ -91,6 +91,14 @@ class MasterSlaveConnection extends Connection
protected $connections = array('master' => null, 'slave' => null);
/**
+ * You can keep the slave connection and then switch back to it
+ * during the request if you know what you are doing.
+ *
+ * @var bool
+ */
+ protected $keepSlave = false;
+
+ /**
* Create Master Slave Connection
*
* @param array $params
@@ -112,6 +120,8 @@ public function __construct(array $params, Driver $driver, Configuration $config
$params['slaves'][$slaveKey]['driver'] = $params['driver'];
}
+ $this->keepSlave = isset($params['keepSlave']) ? (bool)$params['keepSlave'] : false;
+
parent::__construct($params, $driver, $config, $eventManager);
}
@@ -152,11 +162,15 @@ public function connect($connectionName = 'slave')
if ($connectionName === 'master') {
/** Set slave connection to master to avoid invalid reads */
- if ($this->connections['slave']) {
+ if ($this->connections['slave'] && ! $this->keepSlave) {
unset($this->connections['slave']);
}
- $this->connections['master'] = $this->connections['slave'] = $this->_conn = $this->connectTo($connectionName);
+ $this->connections['master'] = $this->_conn = $this->connectTo($connectionName);
+
+ if ( ! $this->keepSlave) {
+ $this->connections['slave'] = $this->connections['master'];
+ }
} else {
$this->connections['slave'] = $this->_conn = $this->connectTo($connectionName);
}

0 comments on commit 4eaa44c

Please sign in to comment.