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
22 namespace Wikimedia\Rdbms
;
25 * Database connection manager.
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.
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).
42 * @author Daniel Kinzler
45 class SessionConsistentConnectionManager
extends ConnectionManager
{
50 private $forceWriteConnection = false;
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.
58 public function prepareForUpdates() {
59 $this->forceWriteConnection
= true;
65 * @param string[]|null $groups
69 public function getReadConnection( array $groups = null ) {
70 if ( $this->forceWriteConnection
) {
71 return parent
::getWriteConnection();
74 return parent
::getReadConnection( $groups );
82 public function getWriteConnection() {
83 $this->prepareForUpdates();
84 return parent
::getWriteConnection();
90 * @param string[]|null $groups
94 public function getReadConnectionRef( array $groups = null ) {
95 if ( $this->forceWriteConnection
) {
96 return parent
::getWriteConnectionRef();
99 return parent
::getReadConnectionRef( $groups );
107 public function getWriteConnectionRef() {
108 $this->prepareForUpdates();
109 return parent
::getWriteConnectionRef();