Merge "mw.htmlform: Fields hidden with 'hide-if' should be disabled"
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManager.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use Database;
6 use DBConnRef;
7
8 /**
9 * Database connection manager.
10 *
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.
14 *
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).
23 *
24 * @since 1.29
25 *
26 * @license GPL-2.0+
27 * @author Daniel Kinzler
28 * @author Addshore
29 */
30 class SessionConsistentConnectionManager extends ConnectionManager {
31
32 /**
33 * @var bool
34 */
35 private $forceWriteConnection = false;
36
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.
40 *
41 * @since 1.29
42 */
43 public function prepareForUpdates() {
44 $this->forceWriteConnection = true;
45 }
46
47 /**
48 * @since 1.29
49 *
50 * @param string[]|null $groups
51 *
52 * @return Database
53 */
54 public function getReadConnection( array $groups = null ) {
55 if ( $this->forceWriteConnection ) {
56 return parent::getWriteConnection();
57 }
58
59 return parent::getReadConnection( $groups );
60 }
61
62 /**
63 * @since 1.29
64 *
65 * @return Database
66 */
67 public function getWriteConnection() {
68 $this->prepareForUpdates();
69 return parent::getWriteConnection();
70 }
71
72 /**
73 * @since 1.29
74 *
75 * @param string[]|null $groups
76 *
77 * @return DBConnRef
78 */
79 public function getReadConnectionRef( array $groups = null ) {
80 if ( $this->forceWriteConnection ) {
81 return parent::getWriteConnectionRef();
82 }
83
84 return parent::getReadConnectionRef( $groups );
85 }
86
87 /**
88 * @since 1.29
89 *
90 * @return DBConnRef
91 */
92 public function getWriteConnectionRef() {
93 $this->prepareForUpdates();
94 return parent::getWriteConnectionRef();
95 }
96
97 }