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
20 namespace Wikimedia\Rdbms
;
22 use InvalidArgumentException
;
25 * Trivial LoadBalancer that always returns an injected connection handle.
29 class LoadBalancerSingle
extends LoadBalancer
{
34 * You probably want to use {@link newFromConnection} instead.
36 * @param array $params An associative array with one member:
37 * - connection: An IDatabase connection object
39 public function __construct( array $params ) {
40 /** @var Database $conn */
41 $conn = $params['connection'] ??
null;
43 throw new InvalidArgumentException( "Missing 'connection' argument." );
48 parent
::__construct( [
50 'type' => $conn->getType(),
51 'host' => $conn->getServer(),
52 'dbname' => $conn->getDBname(),
55 'trxProfiler' => $params['trxProfiler'] ??
null,
56 'srvCache' => $params['srvCache'] ??
null,
57 'wanCache' => $params['wanCache'] ??
null,
58 'localDomain' => $params['localDomain'] ??
$this->conn
->getDomainID(),
59 'readOnlyReason' => $params['readOnlyReason'] ??
false,
60 'clusterName' => $params['clusterName'] ??
null,
63 if ( isset( $params['readOnlyReason'] ) ) {
64 $conn->setLBInfo( $conn::LB_READ_ONLY_REASON
, $params['readOnlyReason'] );
69 * @param IDatabase $db Live connection handle
70 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
71 * @return LoadBalancerSingle
74 public static function newFromConnection( IDatabase
$db, array $params = [] ) {
75 return new static( array_merge(
76 [ 'localDomain' => $db->getDomainID() ],
78 [ 'connection' => $db ]
82 protected function sanitizeConnectionFlags( $flags, $domain ) {
83 // There is only one underlying connection handle. Also, this class is only meant to
84 // be used during situations like site installation, where there should be no contenting
85 // connections, and integration testing, where everything uses temporary tables.
86 $flags &= ~self
::CONN_TRX_AUTOCOMMIT
;
91 protected function reallyOpenConnection( $i, DatabaseDomain
$domain, array $lbInfo ) {
92 foreach ( $lbInfo as $k => $v ) {
93 $this->conn
->setLBInfo( $k, $v );
99 public function __destruct() {
100 // do nothing since the connection was injected