3 namespace Wikimedia\Rdbms
;
6 * Database connection manager.
8 * This manages access to master and replica databases. It also manages state that indicates whether
9 * the replica databases are possibly outdated after a write operation, and thus the master database
10 * should be used for subsequent read operations.
12 * @note: Services that access overlapping sets of database tables, or interact with logically
13 * related sets of data in the database, should share a SessionConsistentConnectionManager.
14 * Services accessing unrelated sets of information may prefer to not share a
15 * SessionConsistentConnectionManager, so they can still perform read operations against replica
16 * databases after a (unrelated, per the assumption) write operation to the master database.
17 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
18 * conditions due to replication lag), but can reduce performance (by directing more read
19 * operations to the master database server).
24 * @author Daniel Kinzler
27 class SessionConsistentConnectionManager
extends ConnectionManager
{
32 private $forceWriteConnection = false;
35 * Forces all future calls to getReadConnection() to return a write connection.
36 * Use this before performing read operations that are critical for a future update.
40 public function prepareForUpdates() {
41 $this->forceWriteConnection
= true;
47 * @param string[]|null $groups
51 public function getReadConnection( array $groups = null ) {
52 if ( $this->forceWriteConnection
) {
53 return parent
::getWriteConnection();
56 return parent
::getReadConnection( $groups );
64 public function getWriteConnection() {
65 $this->prepareForUpdates();
66 return parent
::getWriteConnection();
72 * @param string[]|null $groups
76 public function getReadConnectionRef( array $groups = null ) {
77 if ( $this->forceWriteConnection
) {
78 return parent
::getWriteConnectionRef();
81 return parent
::getReadConnectionRef( $groups );
89 public function getWriteConnectionRef() {
90 $this->prepareForUpdates();
91 return parent
::getWriteConnectionRef();