$this->replicaOnly = !empty( $params['slaveOnly'] );
}
- protected function getSeparateMainLB() {
- global $wgDBtype;
-
- if ( $this->usesMainDB() && $wgDBtype !== 'sqlite' ) {
- if ( !$this->separateMainLB ) {
- // We must keep a separate connection to MySQL in order to avoid deadlocks
- $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
- $this->separateMainLB = $lbFactory->newMainLB();
- }
- return $this->separateMainLB;
- } else {
- // However, SQLite has an opposite behavior due to DB-level locking
- return null;
- }
- }
-
/**
* Get a connection to the specified database
*
* @param int $serverIndex
- * @return IDatabase
+ * @return Database
* @throws MWException
*/
protected function getDB( $serverIndex ) {
throw $this->connFailureErrors[$serverIndex];
}
- # If server connection info was given, use that
if ( $this->serverInfos ) {
+ // Use custom database defined by server connection info
$info = $this->serverInfos[$serverIndex];
$type = isset( $info['type'] ) ? $info['type'] : 'mysql';
$host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
// Use a blank trx profiler to ignore expections as this is a cache
$info['trxProfiler'] = new TransactionProfiler();
$db = Database::factory( $type, $info );
- $db->clearFlag( DBO_TRX );
+ $db->clearFlag( DBO_TRX ); // auto-commit mode
} else {
+ // Use the main LB database
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$index = $this->replicaOnly ? DB_REPLICA : DB_MASTER;
- if ( $this->getSeparateMainLB() ) {
- $db = $this->getSeparateMainLB()->getConnection( $index );
- $db->clearFlag( DBO_TRX ); // auto-commit mode
+ if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) {
+ // Keep a separate connection to avoid contention and deadlocks
+ $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTO );
+ // @TODO: Use a blank trx profiler to ignore expections as this is a cache
} else {
- $db = wfGetDB( $index );
- // Can't mess with transaction rounds (DBO_TRX) :(
+ // However, SQLite has the opposite behavior due to DB-level locking.
+ // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead.
+ $db = $lb->getConnection( $index );
}
}
+
$this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
$this->conns[$serverIndex] = $db;
}
* On typical message and page data, this can provide a 3X decrease
* in storage requirements.
*
- * @param mixed $data
+ * @param mixed &$data
* @return string
*/
protected function serialize( &$data ) {
return true;
}
- $lb = $this->getSeparateMainLB()
- ?: MediaWikiServices::getInstance()->getDBLoadBalancer();
-
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
if ( $lb->getServerCount() <= 1 ) {
return true; // no replica DBs
}