Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Database
20 */
21
22 namespace Wikimedia\Rdbms;
23
24 /**
25 * Database connection manager.
26 *
27 * This manages access to master and replica databases. It also manages state that indicates whether
28 * the replica databases are possibly outdated after a write operation, and thus the master database
29 * should be used for subsequent read operations.
30 *
31 * @note: Services that access overlapping sets of database tables, or interact with logically
32 * related sets of data in the database, should share a SessionConsistentConnectionManager.
33 * Services accessing unrelated sets of information may prefer to not share a
34 * SessionConsistentConnectionManager, so they can still perform read operations against replica
35 * databases after a (unrelated, per the assumption) write operation to the master database.
36 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
37 * conditions due to replication lag), but can reduce performance (by directing more read
38 * operations to the master database server).
39 *
40 * @since 1.29
41 *
42 * @author Daniel Kinzler
43 * @author Addshore
44 */
45 class SessionConsistentConnectionManager extends ConnectionManager {
46
47 /**
48 * @var bool
49 */
50 private $forceWriteConnection = false;
51
52 /**
53 * Forces all future calls to getReadConnection() to return a write connection.
54 * Use this before performing read operations that are critical for a future update.
55 *
56 * @since 1.29
57 */
58 public function prepareForUpdates() {
59 $this->forceWriteConnection = true;
60 }
61
62 /**
63 * @since 1.29
64 *
65 * @param string[]|null $groups
66 *
67 * @return IDatabase
68 */
69 public function getReadConnection( array $groups = null ) {
70 if ( $this->forceWriteConnection ) {
71 return parent::getWriteConnection();
72 }
73
74 return parent::getReadConnection( $groups );
75 }
76
77 /**
78 * @since 1.29
79 *
80 * @return IDatabase
81 */
82 public function getWriteConnection() {
83 $this->prepareForUpdates();
84 return parent::getWriteConnection();
85 }
86
87 /**
88 * @since 1.29
89 *
90 * @param string[]|null $groups
91 *
92 * @return DBConnRef
93 */
94 public function getReadConnectionRef( array $groups = null ) {
95 if ( $this->forceWriteConnection ) {
96 return parent::getWriteConnectionRef();
97 }
98
99 return parent::getReadConnectionRef( $groups );
100 }
101
102 /**
103 * @since 1.29
104 *
105 * @return DBConnRef
106 */
107 public function getWriteConnectionRef() {
108 $this->prepareForUpdates();
109 return parent::getWriteConnectionRef();
110 }
111
112 }