Commit 4eaa44c0 authored by Benjamin Eberlei's avatar Benjamin Eberlei

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

Add option to keep the slave in master/slave setups. You can call $conn->connectTo("slave") to get back to the slave connection
parent 4eefceb0
...@@ -90,6 +90,14 @@ class MasterSlaveConnection extends Connection ...@@ -90,6 +90,14 @@ class MasterSlaveConnection extends Connection
*/ */
protected $connections = array('master' => null, 'slave' => null); 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 * Create Master Slave Connection
* *
...@@ -112,6 +120,8 @@ class MasterSlaveConnection extends Connection ...@@ -112,6 +120,8 @@ class MasterSlaveConnection extends Connection
$params['slaves'][$slaveKey]['driver'] = $params['driver']; $params['slaves'][$slaveKey]['driver'] = $params['driver'];
} }
$this->keepSlave = isset($params['keepSlave']) ? (bool)$params['keepSlave'] : false;
parent::__construct($params, $driver, $config, $eventManager); parent::__construct($params, $driver, $config, $eventManager);
} }
...@@ -152,11 +162,15 @@ class MasterSlaveConnection extends Connection ...@@ -152,11 +162,15 @@ class MasterSlaveConnection extends Connection
if ($connectionName === 'master') { if ($connectionName === 'master') {
/** Set slave connection to master to avoid invalid reads */ /** Set slave connection to master to avoid invalid reads */
if ($this->connections['slave']) { if ($this->connections['slave'] && ! $this->keepSlave) {
unset($this->connections['slave']); 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 { } else {
$this->connections['slave'] = $this->_conn = $this->connectTo($connectionName); $this->connections['slave'] = $this->_conn = $this->connectTo($connectionName);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment