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.
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.
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
20 namespace Wikimedia\Rdbms
;
23 * Database connection manager.
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.
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).
40 * @author Daniel Kinzler
43 class SessionConsistentConnectionManager
extends ConnectionManager
{
48 private $forceWriteConnection = false;
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.
56 public function prepareForUpdates() {
57 $this->forceWriteConnection
= true;
62 * @since 1.37 Added optional $flags parameter
64 * @param string[]|null $groups
67 * @return IReadableDatabase
69 public function getReadConnection( ?
array $groups = null, int $flags = 0 ) {
70 if ( $this->forceWriteConnection
) {
71 return parent
::getWriteConnection( $flags );
74 return parent
::getReadConnection( $groups, $flags );
79 * @since 1.37 Added optional $flags parameter
85 public function getWriteConnection( int $flags = 0 ) {
86 $this->prepareForUpdates();
87 return parent
::getWriteConnection( $flags );