MediaWiki master
ConnectionManager.php
Go to the documentation of this file.
1<?php
6namespace Wikimedia\Rdbms;
7
8use InvalidArgumentException;
9
20
24 private $loadBalancer;
25
31 private $domain;
32
36 private $groups = [];
37
46 public function __construct( ILoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
47 if ( !is_string( $domain ) && $domain !== false ) {
48 throw new InvalidArgumentException( '$dbName must be a string, or false.' );
49 }
50
51 $this->loadBalancer = $loadBalancer;
52 $this->domain = $domain;
53 $this->groups = $groups;
54 }
55
62 private function getConnection( $i, ?array $groups = null, int $flags = 0 ) {
63 $groups ??= $this->groups;
64 return $this->loadBalancer->getConnection( $i, $groups, $this->domain, $flags );
65 }
66
75 public function getWriteConnection( int $flags = 0 ) {
76 return $this->getConnection( DB_PRIMARY, null, $flags );
77 }
78
88 public function getReadConnection( ?array $groups = null, int $flags = 0 ) {
89 $groups ??= $this->groups;
90 return $this->getConnection( DB_REPLICA, $groups, $flags );
91 }
92}
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28
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.