3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 namespace Wikimedia\Rdbms
;
24 use InvalidArgumentException
;
27 * Database connection manager.
29 * This manages access to master and replica databases.
35 class ConnectionManager
{
40 private $loadBalancer;
43 * The symbolic name of the target database, or false for the local wiki's database.
55 * @param ILoadBalancer $loadBalancer
56 * @param string|bool $domain Optional logical DB name, defaults to current wiki.
57 * This follows the convention for database names used by $loadBalancer.
58 * @param string[] $groups see LoadBalancer::getConnection
60 * @throws InvalidArgumentException
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.' );
67 $this->loadBalancer
= $loadBalancer;
68 $this->domain
= $domain;
69 $this->groups
= $groups;
74 * @param string[]|null $groups
78 private function getConnection( $i, array $groups = null ) {
79 $groups = $groups === null ?
$this->groups
: $groups;
80 return $this->loadBalancer
->getConnection( $i, $groups, $this->domain
);
85 * @param string[]|null $groups
89 private function getConnectionRef( $i, array $groups = null ) {
90 $groups = $groups === null ?
$this->groups
: $groups;
91 return $this->loadBalancer
->getConnectionRef( $i, $groups, $this->domain
);
95 * Returns a connection to the master DB, for updating. The connection should later be released
96 * by calling releaseConnection().
102 public function getWriteConnection() {
103 return $this->getConnection( DB_MASTER
);
107 * Returns a database connection for reading. The connection should later be released by
108 * calling releaseConnection().
112 * @param string[]|null $groups
116 public function getReadConnection( array $groups = null ) {
117 $groups = $groups === null ?
$this->groups
: $groups;
118 return $this->getConnection( DB_REPLICA
, $groups );
124 * @param IDatabase $db
126 public function releaseConnection( IDatabase
$db ) {
127 $this->loadBalancer
->reuseConnection( $db );
131 * Returns a connection ref to the master DB, for updating.
137 public function getWriteConnectionRef() {
138 return $this->getConnectionRef( DB_MASTER
);
142 * Returns a database connection ref for reading.
146 * @param string[]|null $groups
150 public function getReadConnectionRef( array $groups = null ) {
151 $groups = $groups === null ?
$this->groups
: $groups;
152 return $this->getConnectionRef( DB_REPLICA
, $groups );