$config = MediaWikiServices::getInstance()->getMainConfig();
$lb = self::getLB();
- $dbr = $lb->getConnection( DB_REPLICA );
+ $dbr = $lb->getConnectionRef( DB_REPLICA );
wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
$row = self::doLoadFromDB( $dbr );
if ( !self::isRowSane( $row ) && $lb->hasOrMadeRecentMasterChanges() ) {
// Might have just been initialized during this request? Underflow?
wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
- $row = self::doLoadFromDB( $lb->getConnection( DB_MASTER ) );
+ $row = self::doLoadFromDB( $lb->getConnectionRef( DB_MASTER ) );
}
if ( !self::isRowSane( $row ) ) {
SiteStatsInit::doAllAndCommit( $dbr );
}
- $row = self::doLoadFromDB( $lb->getConnection( DB_MASTER ) );
+ $row = self::doLoadFromDB( $lb->getConnectionRef( DB_MASTER ) );
}
if ( !self::isRowSane( $row ) ) {
$cache->makeKey( 'SiteStats', 'groupcounts', $group ),
$cache::TTL_HOUR,
function ( $oldValue, &$ttl, array &$setOpts ) use ( $group, $fname ) {
- $dbr = self::getLB()->getConnection( DB_REPLICA );
+ $dbr = self::getLB()->getConnectionRef( DB_REPLICA );
$setOpts += Database::getCacheSetOptions( $dbr );
return (int)$dbr->selectField(
$cache->makeKey( 'SiteStats', 'page-in-namespace', $ns ),
$cache::TTL_HOUR,
function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns, $fname ) {
- $dbr = self::getLB()->getConnection( DB_REPLICA );
+ $dbr = self::getLB()->getConnectionRef( DB_REPLICA );
$setOpts += Database::getCacheSetOptions( $dbr );
return (int)$dbr->selectField(
// the stash request finishes parsing. For the lock acquisition below, there is not much
// need to duplicate parsing of the same content/user/summary bundle, so try to avoid
// blocking at all here.
- $dbw = $this->lb->getConnection( DB_MASTER );
+ $dbw = $this->lb->getConnectionRef( DB_MASTER );
if ( !$dbw->lock( $key, $fname, 0 ) ) {
// De-duplicate requests on the same key
return self::ERROR_BUSY;
* @return string|null TS_MW timestamp or null
*/
private function lastEditTime( User $user ) {
- $db = $this->lb->getConnection( DB_REPLICA );
+ $db = $this->lb->getConnectionRef( DB_REPLICA );
+
$actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
$time = $db->selectField(
[ 'recentchanges' ] + $actorQuery['tables'],
return [];
}
- $db = $db ?: $this->loadBalancer->getConnection( DB_REPLICA );
+ $db = $db ?: $this->loadBalancer->getConnectionRef( DB_REPLICA );
$result = $db->select(
[ 'ipblocks_restrictions', 'page' ],
return false;
}
- $dbw = $this->loadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
$dbw->insert(
'ipblocks_restrictions',
* @return bool
*/
public function update( array $restrictions ) {
- $dbw = $this->loadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
$dbw->startAtomic( __METHOD__ );
$parentBlockId = (int)$parentBlockId;
- $db = $this->loadBalancer->getConnection( DB_MASTER );
+ $db = $this->loadBalancer->getConnectionRef( DB_MASTER );
$db->startAtomic( __METHOD__ );
* @return bool
*/
public function delete( array $restrictions ) {
- $dbw = $this->loadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
$result = true;
foreach ( $restrictions as $restriction ) {
if ( !$restriction instanceof Restriction ) {
* @return bool
*/
public function deleteByBlockId( $blockId ) {
- $dbw = $this->loadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
return $dbw->delete(
'ipblocks_restrictions',
[ 'ir_ipb_id' => $blockId ],
* @return bool
*/
public function deleteByParentBlockId( $parentBlockId ) {
- $dbw = $this->loadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
return $dbw->deleteJoin(
'ipblocks_restrictions',
'ipblocks',
$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
- $dbw = $services->getDBLoadBalancer()->getConnection( DB_MASTER );
+ $dbw = $services->getDBLoadBalancer()->getConnectionRef( DB_MASTER );
$lockKey = $dbw->getDomainID() . ':site_stats'; // prepend wiki ID
$pd = [];
if ( $config->get( 'SiteStatsAsyncFactor' ) ) {
$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
- $dbr = $services->getDBLoadBalancer()->getConnection( DB_REPLICA, 'vslow' );
+ $dbr = $services->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, 'vslow' );
# Get non-bot users than did some recent action other than making accounts.
# If account creation is included, the number gets inflated ~20+ fold on enwiki.
$rcQuery = RecentChange::getQueryInfo();
*/
public function doUpdate() {
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
- $dbw = $lb->getConnection( DB_MASTER );
+ $dbw = $lb->getConnectionRef( DB_MASTER );
$fname = __METHOD__;
( new AutoCommitUpdate( $dbw, __METHOD__, function () use ( $lb, $dbw, $fname ) {
// The user_editcount is probably NULL (e.g. not initialized).
// Since this update runs after the new revisions were committed,
// wait for the replica DB to catch up so they will be counted.
- $dbr = $lb->getConnection( DB_REPLICA );
- // If $dbr is actually the master DB, then clearing the snapshot is
+ $dbr = $lb->getConnectionRef( DB_REPLICA );
+ // If $dbr is actually the master DB, then clearing the snapshot
// is harmless and waitForMasterPos() will just no-op.
$dbr->flushSnapshot( $fname );
$lb->waitForMasterPos( $dbr );
public function run() {
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
$lb = $lbFactory->getMainLB();
- $dbw = $lb->getConnection( DB_MASTER );
+ $dbw = $lb->getConnectionRef( DB_MASTER );
$this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
}
// Cut down on the time spent in waitForMasterPos() in the critical section
- $dbr = $lb->getConnection( DB_REPLICA, [ 'recentchanges' ] );
+ $dbr = $lb->getConnectionRef( DB_REPLICA, [ 'recentchanges' ] );
if ( !$lb->waitForMasterPos( $dbr ) ) {
$this->setLastError( "Timed out while pre-waiting for replica DB to catch up" );
return false;
$batchSize = $wgUpdateRowsPerQuery;
$loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
- $dbw = $loadBalancer->getConnection( DB_MASTER );
- $dbr = $loadBalancer->getConnection( DB_REPLICA, [ 'watchlist' ] );
+ $dbw = $loadBalancer->getConnectionRef( DB_MASTER );
+ $dbr = $loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
// Wait before lock to try to reduce time waiting in the lock.
if ( !$loadBalancer->waitForMasterPos( $dbr ) ) {
$lbFactory = $services->getDBLoadBalancerFactory();
$rowsPerQuery = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
- $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
+ $dbw = $lbFactory->getMainLB()->getConnectionRef( DB_MASTER );
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
$timestamp = $this->params['timestamp'] ?? null;
if ( $timestamp === null ) {
// Serialize link update job by page ID so they see each others' changes.
// The page ID and latest revision ID will be queried again after the lock
// is acquired to bail if they are changed from that of loadPageData() above.
- $dbw = $lbFactory->getMainLB()->getConnection( DB_MASTER );
+ // Serialize links updates by page ID so they see each others' changes
+ $dbw = $lbFactory->getMainLB()->getConnectionRef( DB_MASTER );
+ /** @noinspection PhpUnusedLocalVariableInspection */
$scopedLock = LinksUpdate::acquirePageLock( $dbw, $page->getId(), 'job' );
if ( $scopedLock === null ) {
// Another job is already updating the page, likely for a prior revision (T170596)
$baseRevId = null;
if ( $edittime && $sectionId !== 'new' ) {
$lb = $this->getDBLoadBalancer();
- $dbr = $lb->getConnection( DB_REPLICA );
+ $dbr = $lb->getConnectionRef( DB_REPLICA );
$rev = Revision::loadFromTimestamp( $dbr, $this->mTitle, $edittime );
// Try the master if this thread may have just added it.
// This could be abstracted into a Revision method, but we don't want
&& $lb->getServerCount() > 1
&& $lb->hasOrMadeRecentMasterChanges()
) {
- $dbw = $lb->getConnection( DB_MASTER );
+ $dbw = $lb->getConnectionRef( DB_MASTER );
$rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
}
if ( $rev ) {
* @param string $text
*/
function update( $id, $title, $text ) {
- $dbw = $this->lb->getConnection( DB_MASTER );
+ $dbw = $this->lb->getMaintenanceConnectionRef( DB_MASTER );
$dbw->replace( 'searchindex',
[ 'si_page' ],
[
* Whether fulltext search is supported by current schema
* @return bool
*/
- function fulltextSearchSupported() {
+ private function fulltextSearchSupported() {
+ // Avoid getConnectionRef() in order to get DatabaseSqlite specifically
/** @var DatabaseSqlite $dbr */
$dbr = $this->lb->getConnection( DB_REPLICA );
-
- return $dbr->checkForEnabledSearch();
+ try {
+ return $dbr->checkForEnabledSearch();
+ } finally {
+ $this->lb->reuseConnection( $dbr );
+ }
}
/**
* @param string $title
* @param string $text
*/
- function update( $id, $title, $text ) {
+ public function update( $id, $title, $text ) {
if ( !$this->fulltextSearchSupported() ) {
return;
}
* @param int $id
* @param string $title
*/
- function updateTitle( $id, $title ) {
+ public function updateTitle( $id, $title ) {
if ( !$this->fulltextSearchSupported() ) {
return;
}
protected function loadSites() {
$this->sites = new SiteList();
- $dbr = $this->dbLoadBalancer->getConnection( DB_REPLICA );
+ $dbr = $this->dbLoadBalancer->getConnectionRef( DB_REPLICA );
$res = $dbr->select(
'sites',
return true;
}
- $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->dbLoadBalancer->getConnectionRef( DB_MASTER );
$dbw->startAtomic( __METHOD__ );
* @return bool Success
*/
public function clear() {
- $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
+ $dbw = $this->dbLoadBalancer->getConnectionRef( DB_MASTER );
$dbw->startAtomic( __METHOD__ );
$ok = $dbw->delete( 'sites', '*', __METHOD__ );
if ( $mode === 'refresh' ) {
$cache->delete( $key, 1 ); // low tombstone/"hold-off" TTL
} else {
- $lb->getConnection( DB_MASTER )->onTransactionPreCommitOrIdle(
+ $lb->getConnectionRef( DB_MASTER )->onTransactionPreCommitOrIdle(
function () use ( $cache, $key ) {
$cache->delete( $key );
},
$lbFactory = $services->getDBLoadBalancerFactory();
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
- $dbw = $services->getDBLoadBalancer()->getConnection( DB_MASTER );
+ $dbw = $services->getDBLoadBalancer()->getConnectionRef( DB_MASTER );
$lockKey = "{$dbw->getDomainID()}:UserGroupMembership:purge"; // per-wiki
$scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 0 );