Debian Codegraph @

includes/libs/rdbms/connectionmanager/SessionConsistentConnectionManager.php

raw symbols SCIP
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 */
20 namespace Wikimedia\Rdbms;
21
22 /**
23 * Database connection manager.
24 *
25 * This manages access to primary and replica databases. It also manages state that indicates whether
26 * the replica databases are possibly outdated after a write operation, and thus the primary database
27 * should be used for subsequent read operations.
28 *
29 * @note: Services that access overlapping sets of database tables, or interact with logically
30 * related sets of data in the database, should share a SessionConsistentConnectionManager.
31 * Services accessing unrelated sets of information may prefer to not share a
32 * SessionConsistentConnectionManager, so they can still perform read operations against replica
33 * databases after a (unrelated, per the assumption) write operation to the primary database.
34 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
35 * conditions due to replication lag), but can reduce performance (by directing more read
36 * operations to the primary database server).
37 *
38 * @since 1.29
39 * @ingroup Database
40 * @author Daniel Kinzler
41 * @author Addshore
42 */
43 class SessionConsistentConnectionManager extends ConnectionManager {
44
45 /**
46 * @var bool
47 */
48 private $forceWriteConnection = false;
49
50 /**
51 * Forces all future calls to getReadConnection() to return a write connection.
52 * Use this before performing read operations that are critical for a future update.
53 *
54 * @since 1.29
55 */
56 public function prepareForUpdates() {
57 $this->forceWriteConnection = true;
58 }
59
60 /**
61 * @since 1.29
62 * @since 1.37 Added optional $flags parameter
63 *
64 * @param string[]|null $groups
65 * @param int $flags
66 *
67 * @return IReadableDatabase
68 */
69 public function getReadConnection( ?array $groups = null, int $flags = 0 ) {
70 if ( $this->forceWriteConnection ) {
71 return parent::getWriteConnection( $flags );
72 }
73
74 return parent::getReadConnection( $groups, $flags );
75 }
76
77 /**
78 * @since 1.29
79 * @since 1.37 Added optional $flags parameter
80 *
81 * @param int $flags
82 *
83 * @return IDatabase
84 */
85 public function getWriteConnection( int $flags = 0 ) {
86 $this->prepareForUpdates();
87 return parent::getWriteConnection( $flags );
88 }
89
90 }