Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManager.php
blobfb031822e71dbe63a5137a8d2b25aa389545b6ef
1 <?php
3 namespace Wikimedia\Rdbms;
5 use Database;
6 use DBConnRef;
8 /**
9 * Database connection manager.
11 * This manages access to master and replica databases. It also manages state that indicates whether
12 * the replica databases are possibly outdated after a write operation, and thus the master database
13 * should be used for subsequent read operations.
15 * @note: Services that access overlapping sets of database tables, or interact with logically
16 * related sets of data in the database, should share a SessionConsistentConnectionManager.
17 * Services accessing unrelated sets of information may prefer to not share a
18 * SessionConsistentConnectionManager, so they can still perform read operations against replica
19 * databases after a (unrelated, per the assumption) write operation to the master database.
20 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
21 * conditions due to replication lag), but can reduce performance (by directing more read
22 * operations to the master database server).
24 * @since 1.29
26 * @license GPL-2.0+
27 * @author Daniel Kinzler
28 * @author Addshore
30 class SessionConsistentConnectionManager extends ConnectionManager {
32 /**
33 * @var bool
35 private $forceWriteConnection = false;
37 /**
38 * Forces all future calls to getReadConnection() to return a write connection.
39 * Use this before performing read operations that are critical for a future update.
41 * @since 1.29
43 public function prepareForUpdates() {
44 $this->forceWriteConnection = true;
47 /**
48 * @since 1.29
50 * @param string[]|null $groups
52 * @return Database
54 public function getReadConnection( array $groups = null ) {
55 if ( $this->forceWriteConnection ) {
56 return parent::getWriteConnection();
59 return parent::getReadConnection( $groups );
62 /**
63 * @since 1.29
65 * @return Database
67 public function getWriteConnection() {
68 $this->prepareForUpdates();
69 return parent::getWriteConnection();
72 /**
73 * @since 1.29
75 * @param string[]|null $groups
77 * @return DBConnRef
79 public function getReadConnectionRef( array $groups = null ) {
80 if ( $this->forceWriteConnection ) {
81 return parent::getWriteConnectionRef();
84 return parent::getReadConnectionRef( $groups );
87 /**
88 * @since 1.29
90 * @return DBConnRef
92 public function getWriteConnectionRef() {
93 $this->prepareForUpdates();
94 return parent::getWriteConnectionRef();