3 namespace Wikimedia\Rdbms
;
9 * Database connection manager.
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.
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).
27 * @author Daniel Kinzler
30 class SessionConsistentConnectionManager
extends ConnectionManager
{
35 private $forceWriteConnection = false;
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.
43 public function prepareForUpdates() {
44 $this->forceWriteConnection
= true;
50 * @param string[]|null $groups
54 public function getReadConnection( array $groups = null ) {
55 if ( $this->forceWriteConnection
) {
56 return parent
::getWriteConnection();
59 return parent
::getReadConnection( $groups );
67 public function getWriteConnection() {
68 $this->prepareForUpdates();
69 return parent
::getWriteConnection();
75 * @param string[]|null $groups
79 public function getReadConnectionRef( array $groups = null ) {
80 if ( $this->forceWriteConnection
) {
81 return parent
::getWriteConnectionRef();
84 return parent
::getReadConnectionRef( $groups );
92 public function getWriteConnectionRef() {
93 $this->prepareForUpdates();
94 return parent
::getWriteConnectionRef();