3 * Simple generator of database connections that always returns the same object.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace Wikimedia\Rdbms
;
26 use InvalidArgumentException
;
29 * Trivial LoadBalancer that always returns an injected connection handle.
31 * Note that, while this LoadBalancer does not open any connections itself,
32 * it still closes the injected connection at times, including during destruction.
33 * It is therefore unsuitable for use in tests unless you have a Database instance
34 * separate from the main test database (which is expected to stay open).
36 class LoadBalancerSingle
extends LoadBalancer
{
41 * @param array $params An associative array with one member:
42 * - connection: An IDatabase connection object
44 public function __construct( array $params ) {
45 if ( !isset( $params['connection'] ) ) {
46 throw new InvalidArgumentException( "Missing 'connection' argument." );
49 $this->db
= $params['connection'];
51 parent
::__construct( [
54 'type' => $this->db
->getType(),
55 'host' => $this->db
->getServer(),
56 'dbname' => $this->db
->getDBname(),
60 'trxProfiler' => $params['trxProfiler'] ??
null,
61 'srvCache' => $params['srvCache'] ??
null,
62 'wanCache' => $params['wanCache'] ??
null,
63 'localDomain' => $params['localDomain'] ??
$this->db
->getDomainID(),
64 'readOnlyReason' => $params['readOnlyReason'] ??
false,
67 if ( isset( $params['readOnlyReason'] ) ) {
68 $this->db
->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
73 * @param IDatabase $db Live connection handle
74 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
75 * @return LoadBalancerSingle
78 public static function newFromConnection( IDatabase
$db, array $params = [] ) {
79 return new static( array_merge(
80 [ 'localDomain' => $db->getDomainID() ],
82 [ 'connection' => $db ]
86 protected function reallyOpenConnection( array $server, DatabaseDomain
$domain ) {
90 public function __destruct() {
91 // do nothing since the connection was injected
96 * @deprecated since 1.29
98 class_alias( LoadBalancerSingle
::class, 'LoadBalancerSingle' );