MediaWiki master
ConnectionManager.php
Go to the documentation of this file.
1<?php
20namespace Wikimedia\Rdbms;
21
22use 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
89 public function getWriteConnection( int $flags = 0 ) {
90 return $this->getConnection( DB_PRIMARY, null, $flags );
91 }
92
102 public function getReadConnection( ?array $groups = null, int $flags = 0 ) {
103 $groups ??= $this->groups;
104 return $this->getConnection( DB_REPLICA, $groups, $flags );
105 }
106}
Database connection manager.
__construct(ILoadBalancer $loadBalancer, $domain=false, array $groups=[])
getWriteConnection(int $flags=0)
Returns a connection to the primary DB, for updating.
getReadConnection(?array $groups=null, int $flags=0)
Returns a database connection for reading.
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