* @ingroup Database
*/
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Services\DestructibleService;
use Psr\Log\LoggerInterface;
use MediaWiki\Logger\LoggerFactory;
* An interface for generating database load balancers
* @ingroup Database
*/
-abstract class LBFactory {
+abstract class LBFactory implements DestructibleService {
+
/** @var ChronologyProtector */
protected $chronProt;
/** @var LoggerInterface */
protected $logger;
- /** @var LBFactory */
- private static $instance;
-
/** @var string|bool Reason all LBs are read-only or false if not */
protected $readOnlyReason = false;
$this->logger = LoggerFactory::getInstance( 'DBTransaction' );
}
+ /**
+ * Disables all load balancers. All connections are closed, and any attempt to
+ * open a new connection will result in a DBAccessError.
+ * @see LoadBalancer::disable()
+ */
+ public function destroy() {
+ $this->shutdown();
+ $this->forEachLBCallMethod( 'disable' );
+ }
+
/**
* Disables all access to the load balancer, will cause all database access
* to throw a DBAccessError
*/
public static function disableBackend() {
- global $wgLBFactoryConf;
- self::$instance = new LBFactoryFake( $wgLBFactoryConf );
+ MediaWikiServices::disableStorageBackend();
}
/**
* Get an LBFactory instance
*
+ * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead.
+ *
* @return LBFactory
*/
public static function singleton() {
- global $wgLBFactoryConf;
-
- if ( is_null( self::$instance ) ) {
- $class = self::getLBFactoryClass( $wgLBFactoryConf );
- $config = $wgLBFactoryConf;
- if ( !isset( $config['readOnlyReason'] ) ) {
- $config['readOnlyReason'] = wfConfiguredReadOnlyReason();
- }
- self::$instance = new $class( $config );
- }
-
- return self::$instance;
+ return MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
}
/**
* Returns the LBFactory class to use and the load balancer configuration.
*
+ * @todo instead of this, use a ServiceContainer for managing the different implementations.
+ *
* @param array $config (e.g. $wgLBFactoryConf)
* @return string Class name
*/
/**
* Shut down, close connections and destroy the cached instance.
- */
- public static function destroyInstance() {
- if ( self::$instance ) {
- self::$instance->shutdown();
- self::$instance->forEachLBCallMethod( 'closeAll' );
- self::$instance = null;
- }
- }
-
- /**
- * Set the instance to be the given object
*
- * @param LBFactory $instance
+ * @deprecated since 1.27, use LBFactory::destroy()
*/
- public static function setInstance( $instance ) {
- self::destroyInstance();
- self::$instance = $instance;
+ public static function destroyInstance() {
+ self::singleton()->destroy();
}
/**
} );
} );
- $start = microtime( true );
$this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
- $timeMs = 1000 * ( microtime( true ) - $start );
-
- RequestContext::getMain()->getStats()->timing( "db.commit-masters", $timeMs );
}
/**
class DBAccessError extends MWException {
public function __construct() {
parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
- "This is not allowed." );
+ "This is not allowed, because database access has been disabled." );
}
}