MediaWiki  master
ConnectionManager.php
Go to the documentation of this file.
1 <?php
22 namespace Wikimedia\Rdbms;
23 
24 use InvalidArgumentException;
25 
36 
40  private $loadBalancer;
41 
47  private $domain;
48 
52  private $groups = [];
53 
62  public function __construct( ILoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
63  if ( !is_string( $domain ) && $domain !== false ) {
64  throw new InvalidArgumentException( '$dbName must be a string, or false.' );
65  }
66 
67  $this->loadBalancer = $loadBalancer;
68  $this->domain = $domain;
69  $this->groups = $groups;
70  }
71 
78  private function getConnection( $i, array $groups = null ) {
80  return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
81  }
82 
89  private function getConnectionRef( $i, array $groups = null ) {
91  return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
92  }
93 
102  public function getWriteConnection() {
103  return $this->getConnection( DB_MASTER );
104  }
105 
116  public function getReadConnection( array $groups = null ) {
118  return $this->getConnection( DB_REPLICA, $groups );
119  }
120 
126  public function releaseConnection( IDatabase $db ) {
127  $this->loadBalancer->reuseConnection( $db );
128  }
129 
137  public function getWriteConnectionRef() {
138  return $this->getConnectionRef( DB_MASTER );
139  }
140 
150  public function getReadConnectionRef( array $groups = null ) {
152  return $this->getConnectionRef( DB_REPLICA, $groups );
153  }
154 
155 }
Wikimedia\Rdbms\ConnectionManager\getWriteConnectionRef
getWriteConnectionRef()
Returns a connection ref to the master DB, for updating.
Definition: ConnectionManager.php:137
Wikimedia\Rdbms\ConnectionManager\getReadConnection
getReadConnection(array $groups=null)
Returns a database connection for reading.
Definition: ConnectionManager.php:116
Wikimedia\Rdbms\ConnectionManager\getWriteConnection
getWriteConnection()
Returns a connection to the master DB, for updating.
Definition: ConnectionManager.php:102
Wikimedia\Rdbms\ConnectionManager\releaseConnection
releaseConnection(IDatabase $db)
Definition: ConnectionManager.php:126
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\ConnectionManager\$groups
string[] $groups
Definition: ConnectionManager.php:52
Wikimedia\Rdbms\ConnectionManager\$domain
string false $domain
The symbolic name of the target database, or false for the local wiki's database.
Definition: ConnectionManager.php:47
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Rdbms\ConnectionManager\__construct
__construct(ILoadBalancer $loadBalancer, $domain=false, array $groups=[])
Definition: ConnectionManager.php:62
Wikimedia\Rdbms\ConnectionManager\getConnectionRef
getConnectionRef( $i, array $groups=null)
Definition: ConnectionManager.php:89
Wikimedia\Rdbms\ConnectionManager
Database connection manager.
Definition: ConnectionManager.php:35
Wikimedia\Rdbms\ConnectionManager\getConnection
getConnection( $i, array $groups=null)
Definition: ConnectionManager.php:78
Wikimedia\Rdbms\ConnectionManager\getReadConnectionRef
getReadConnectionRef(array $groups=null)
Returns a database connection ref for reading.
Definition: ConnectionManager.php:150
Wikimedia\Rdbms\ConnectionManager\$loadBalancer
ILoadBalancer $loadBalancer
Definition: ConnectionManager.php:40
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81