3 namespace Wikimedia\Rdbms
;
8 * Database connection manager.
10 * This manages access to master and replica databases. It also manages state that indicates whether
11 * the replica databases are possibly outdated after a write operation, and thus the master database
12 * should be used for subsequent read operations.
14 * @note: Services that access overlapping sets of database tables, or interact with logically
15 * related sets of data in the database, should share a SessionConsistentConnectionManager.
16 * Services accessing unrelated sets of information may prefer to not share a
17 * SessionConsistentConnectionManager, so they can still perform read operations against replica
18 * databases after a (unrelated, per the assumption) write operation to the master database.
19 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
20 * conditions due to replication lag), but can reduce performance (by directing more read
21 * operations to the master database server).
26 * @author Daniel Kinzler
29 class SessionConsistentConnectionManager
extends ConnectionManager
{
34 private $forceWriteConnection = false;
37 * Forces all future calls to getReadConnection() to return a write connection.
38 * Use this before performing read operations that are critical for a future update.
42 public function prepareForUpdates() {
43 $this->forceWriteConnection
= true;
49 * @param string[]|null $groups
53 public function getReadConnection( array $groups = null ) {
54 if ( $this->forceWriteConnection
) {
55 return parent
::getWriteConnection();
58 return parent
::getReadConnection( $groups );
66 public function getWriteConnection() {
67 $this->prepareForUpdates();
68 return parent
::getWriteConnection();
74 * @param string[]|null $groups
78 public function getReadConnectionRef( array $groups = null ) {
79 if ( $this->forceWriteConnection
) {
80 return parent
::getWriteConnectionRef();
83 return parent
::getReadConnectionRef( $groups );
91 public function getWriteConnectionRef() {
92 $this->prepareForUpdates();
93 return parent
::getWriteConnectionRef();