MediaWiki  master
ConnectionManager.php
Go to the documentation of this file.
1 <?php
20 namespace Wikimedia\Rdbms;
21 
22 use InvalidArgumentException;
23 
34 
38  private $loadBalancer;
39 
45  private $domain;
46 
50  private $groups = [];
51 
60  public function __construct( ILoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
61  if ( !is_string( $domain ) && $domain !== false ) {
62  throw new InvalidArgumentException( '$dbName must be a string, or false.' );
63  }
64 
65  $this->loadBalancer = $loadBalancer;
66  $this->domain = $domain;
67  $this->groups = $groups;
68  }
69 
76  private function getConnection( $i, ?array $groups = null, int $flags = 0 ) {
77  $groups ??= $this->groups;
78  return $this->loadBalancer->getConnection( $i, $groups, $this->domain, $flags );
79  }
80 
86  private function getConnectionRef( $i, array $groups = null ) {
87  $groups ??= $this->groups;
88  return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
89  }
90 
99  public function getWriteConnection( int $flags = 0 ) {
100  return $this->getConnection( DB_PRIMARY, null, $flags );
101  }
102 
112  public function getReadConnection( ?array $groups = null, int $flags = 0 ) {
113  $groups ??= $this->groups;
114  return $this->getConnection( DB_REPLICA, $groups, $flags );
115  }
116 
122  public function releaseConnection( IDatabase $db ) {
123  wfDeprecated( 'ConnectionManager::releaseConnection()', '1.38' );
124  $this->loadBalancer->reuseConnection( $db );
125  }
126 
135  public function getWriteConnectionRef() {
136  wfDeprecated( 'ConnectionManager::getWriteConnectionRef()', '1.39' );
137  return $this->getConnectionRef( DB_PRIMARY );
138  }
139 
148  public function getReadConnectionRef( array $groups = null ) {
149  wfDeprecated( 'ConnectionManager::getReadConnectionRef()', '1.38' );
150  $groups ??= $this->groups;
151  return $this->getConnectionRef( DB_REPLICA, $groups );
152  }
153 
162  wfDeprecated( 'ConnectionManager::getLazyWriteConnectionRef()', '1.39' );
163  return $this->getConnectionRef( DB_PRIMARY );
164  }
165 
174  public function getLazyReadConnectionRef( array $groups = null ) {
175  wfDeprecated( 'ConnectionManager::getLazyReadConnectionRef()', '1.39' );
176  $groups ??= $this->groups;
177  return $this->getConnectionRef( DB_REPLICA, $groups );
178  }
179 
180 }
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Database connection manager.
getWriteConnectionRef()
Returns a connection ref to the primary DB, for updating.
__construct(ILoadBalancer $loadBalancer, $domain=false, array $groups=[])
getLazyReadConnectionRef(array $groups=null)
Returns a lazy-connecting database connection ref for reading.
getWriteConnection(int $flags=0)
Returns a connection to the primary DB, for updating.
getReadConnectionRef(array $groups=null)
Returns a database connection ref for reading.
getLazyWriteConnectionRef()
Returns a lazy-connecting database connection ref for updating.
getReadConnection(?array $groups=null, int $flags=0)
Returns a database connection for reading.
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition: DBConnRef.php:29
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
This class is a delegate to ILBFactory for a given database cluster.
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28