Connection can be used with master-slave setups. Important for the understanding of this connection should be how and when it picks the slave or master. 1. Slave if master was never picked before and ONLY if 'getWrappedConnection' or 'executeQuery' is used. 2. Master picked when 'exec', 'executeUpdate', 'insert', 'delete', 'update', 'createSavepoint', 'releaseSavepoint', 'beginTransaction', 'rollback', 'commit', 'query' or 'prepare' is called. 3. If master was picked once during the lifetime of the connection it will always get picked afterwards. 4. One slave connection is randomly picked ONCE during a request. ATTENTION: You can write to the slave with this connection if you execute a write query without opening up a transaction. For example: $conn = DriverManager::getConnection(...); $conn->executeQuery("DELETE FROM table"); Be aware that Connection#executeQuery is a method specifically for READ operations only. This connection is limited to slave operations using the Connection#executeQuery operation only, because it wouldn't be compatible with the ORM or SchemaManager code otherwise. Both use all the other operations in a context where writes could happen to a slave, which makes this restricted approach necessary. You can manually connect to the master at any time by calling: $conn->connect('master'); Instantiation through the DriverManager looks like:
Author: Lars Strojny (lstrojny@php.net)
Author: Benjamin Eberlei (kontakt@beberlei.de)
Inheritance: extends Doctrine\DBAL\Connection
Example #1
0
 /**
  * {@inheritdoc}
  */
 public function connect($connectionName = null)
 {
     try {
         return parent::connect($connectionName);
     } catch (DBALException $e) {
         if ($this->_eventManager->hasListeners('failConnect')) {
             $eventArgs = new FailedConnectionEvent($this, $e);
             $this->_eventManager->dispatchEvent('failConnect', $eventArgs);
         }
     }
 }
 /**
  * @param MasterSlaveConnection $connection
  * @return Success|Failure
  */
 private function checkMasterSlaveConnection(MasterSlaveConnection $connection)
 {
     // TODO Check all slaves, instead of random one if possible.
     $connection->connect('slave');
     $isSlaveConnected = $connection->ping();
     $connection->connect('master');
     $isMasterConnected = $connection->ping();
     $data = ['slave' => $isSlaveConnected ? 'connected' : 'not connected', 'master' => $isMasterConnected ? 'connected' : 'not connected'];
     if ($isMasterConnected && $isSlaveConnected) {
         return new Success(get_class($connection), $data);
     }
     return new Failure(get_class($connection), $data);
 }
 public function testCheckMasterSlaveConnectionFailedSecondPing()
 {
     $this->masterSlaveConnection->method('ping')->willReturnOnConsecutiveCalls(true, false);
     $actual = (new CheckConnection($this->masterSlaveConnection))->check();
     self::assertInstanceOf(Failure::class, $actual);
 }