ConnectionManager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  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.
  7. *
  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.
  12. *
  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
  17. *
  18. * @file
  19. * @ingroup Database
  20. */
  21. namespace Wikimedia\Rdbms;
  22. use InvalidArgumentException;
  23. /**
  24. * Database connection manager.
  25. *
  26. * This manages access to master and replica databases.
  27. *
  28. * @since 1.29
  29. *
  30. * @author Addshore
  31. */
  32. class ConnectionManager {
  33. /**
  34. * @var ILoadBalancer
  35. */
  36. private $loadBalancer;
  37. /**
  38. * The symbolic name of the target database, or false for the local wiki's database.
  39. *
  40. * @var string|false
  41. */
  42. private $domain;
  43. /**
  44. * @var string[]
  45. */
  46. private $groups = [];
  47. /**
  48. * @param ILoadBalancer $loadBalancer
  49. * @param string|bool $domain Optional logical DB name, defaults to current wiki.
  50. * This follows the convention for database names used by $loadBalancer.
  51. * @param string[] $groups see LoadBalancer::getConnection
  52. *
  53. * @throws InvalidArgumentException
  54. */
  55. public function __construct( ILoadBalancer $loadBalancer, $domain = false, array $groups = [] ) {
  56. if ( !is_string( $domain ) && $domain !== false ) {
  57. throw new InvalidArgumentException( '$dbName must be a string, or false.' );
  58. }
  59. $this->loadBalancer = $loadBalancer;
  60. $this->domain = $domain;
  61. $this->groups = $groups;
  62. }
  63. /**
  64. * @param int $i
  65. * @param string[]|null $groups
  66. *
  67. * @return IDatabase
  68. */
  69. private function getConnection( $i, array $groups = null ) {
  70. $groups = $groups === null ? $this->groups : $groups;
  71. return $this->loadBalancer->getConnection( $i, $groups, $this->domain );
  72. }
  73. /**
  74. * @param int $i
  75. * @param string[]|null $groups
  76. *
  77. * @return DBConnRef
  78. */
  79. private function getConnectionRef( $i, array $groups = null ) {
  80. $groups = $groups === null ? $this->groups : $groups;
  81. return $this->loadBalancer->getConnectionRef( $i, $groups, $this->domain );
  82. }
  83. /**
  84. * Returns a connection to the master DB, for updating. The connection should later be released
  85. * by calling releaseConnection().
  86. *
  87. * @since 1.29
  88. *
  89. * @return IDatabase
  90. */
  91. public function getWriteConnection() {
  92. return $this->getConnection( DB_MASTER );
  93. }
  94. /**
  95. * Returns a database connection for reading. The connection should later be released by
  96. * calling releaseConnection().
  97. *
  98. * @since 1.29
  99. *
  100. * @param string[]|null $groups
  101. *
  102. * @return IDatabase
  103. */
  104. public function getReadConnection( array $groups = null ) {
  105. $groups = $groups === null ? $this->groups : $groups;
  106. return $this->getConnection( DB_REPLICA, $groups );
  107. }
  108. /**
  109. * @since 1.29
  110. *
  111. * @param IDatabase $db
  112. */
  113. public function releaseConnection( IDatabase $db ) {
  114. $this->loadBalancer->reuseConnection( $db );
  115. }
  116. /**
  117. * Returns a connection ref to the master DB, for updating.
  118. *
  119. * @since 1.29
  120. *
  121. * @return DBConnRef
  122. */
  123. public function getWriteConnectionRef() {
  124. return $this->getConnectionRef( DB_MASTER );
  125. }
  126. /**
  127. * Returns a database connection ref for reading.
  128. *
  129. * @since 1.29
  130. *
  131. * @param string[]|null $groups
  132. *
  133. * @return DBConnRef
  134. */
  135. public function getReadConnectionRef( array $groups = null ) {
  136. $groups = $groups === null ? $this->groups : $groups;
  137. return $this->getConnectionRef( DB_REPLICA, $groups );
  138. }
  139. }