@ -114,6 +114,43 @@ use yii\caching\CacheInterface;
* @property bool $isActive Whether the DB connection is established. This property is read-only.
* @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the
* sequence object. This property is read-only.
* @property bool $enableReplicas whether to enable read/write splitting by using [[replicas]] to read data.
* Note that if [[replicas]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
* @property array $replicas list of replica connection configurations. Each configuration is used to create a replica DB connection.
* When [[enableReplicas]] is true, one of these configurations will be chosen and used to create a DB connection
* for performing read queries only.
* @property array $replicaConfig the configuration that should be merged with every replica configuration listed in [[replicas]].
* For example,
*
* ```php
* [
* 'username' => 'replica',
* 'password' => 'replica',
* 'attributes' => [
* // use a smaller connection timeout
* PDO::ATTR_TIMEOUT => 10,
* ],
* ]
* ```
* @property array $primaries list of primary connection configurations. Each configuration is used to create a primary DB connection.
* When [[open()]] is called, one of these configurations will be chosen and used to create a DB connection
* which will be used by this object.
* Note that when this property is not empty, the connection setting (e.g. `dsn`, `username`) of this object will
* be ignored.
* @property array $primaryConfig the configuration that should be merged with every primary configuration listed in [[primaries]].
* For example,
*
* ```php
* [
* 'username' => 'primary',
* 'password' => 'primary',
* 'attributes' => [
* // use a smaller connection timeout
* PDO::ATTR_TIMEOUT => 10,
* ],
* ]
* ```
* @property bool $shufflePrimaries whether to shuffle [[primaries]] before getting one.
* @property-read Connection|null $primary The currently active primary connection. `null` is returned if no primary
* connection is available. This property is read-only.
* @property-read PDO $primaryPdo The PDO instance for the currently active primary connection. This property is
@ -128,7 +165,6 @@ use yii\caching\CacheInterface;
* is read-only.
* @property Transaction|null $transaction The currently active transaction. Null if no active transaction.
* This property is read-only.
* @mixin ConnectionDeprecationsTrait
*
* @author Qiang Xue < qiang.xue @ gmail . com >
* @since 2.0
@ -344,53 +380,57 @@ class Connection extends Component
/**
* @var bool whether to enable read/write splitting by using [[replicas]] to read data.
* Note that if [[replicas]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
* @since 2.0.36
* @deprecated since 2.0.36. Use [[enableReplicas]] instead.
*/
public $enableReplica s = true;
public $enableSlave s = true;
/**
* Returns the value of [[enableReplicas]].
* @return bool
* @deprecated since 2.0.36. Use [[enableReplicas]] instead.
* @since 2.0.36
* @internal
*/
public function getEnableSlave s()
public function getEnableReplica s()
{
return $this->enableReplica s;
return $this->enableSlave s;
}
/**
* Sets the value of [[enableReplicas]].
* @param bool $value
* @deprecated since 2.0.36. Use [[enableReplicas]] instead.
* @since 2.0.36
* @internal
*/
public function setEnableSlave s($value)
public function setEnableReplica s($value)
{
$this->enableReplica s = $value;
$this->enableSlave s = $value;
}
/**
* @var array list of replica connection configurations. Each configuration is used to create a replica DB connection.
* When [[enableReplicas]] is true, one of these configurations will be chosen and used to create a DB connection
* for performing read queries only.
* @see enableReplica s
* @see replica Config
* @since 2.0.36
* @see enableSlave s
* @see slave Config
* @deprecated since 2.0.36. Use [[replicas]] instead.
*/
public $replica s = [];
public $slave s = [];
/**
* Returns the value of [[replicas]].
* @return array
* @deprecated since 2.0.36. Use [[replicas]] instead.
* @since 2.0.36
* @internal
*/
public function getSlave s()
public function getReplica s()
{
return $this->replica s;
return $this->slave s;
}
/**
* Sets the value of [[replicas]].
* @param array $value
* @deprecated since 2.0.36. Use [[replicas]] instead.
* @since 2.0.36
* @internal
*/
public function setSlave s($value)
public function setReplica s($value)
{
$this->replica s = $value;
$this->slave s = $value;
}
/**
* @var array the configuration that should be merged with every replica configuration listed in [[replicas]].
@ -407,26 +447,28 @@ class Connection extends Component
* ]
* ```
*
* @since 2.0.36
* @deprecated since 2.0.36. Use [[replicaConfig]] instead.
*/
public $replica Config = [];
public $slave Config = [];
/**
* Returns the value of [[replicaConfig]].
* @return array
* @deprecated since 2.0.36. Use [[replicaConfig]] instead.
* @since 2.0.36
* @internal
*/
public function getSlave Config()
public function getReplica Config()
{
return $this->replica Config;
return $this->slave Config;
}
/**
* Sets the value of [[replicaConfig]].
* @param array $value
* @deprecated since 2.0.36. Use [[replicaConfig]] instead.
* @since 2.0.36
* @internal
*/
public function setSlave Config($value)
public function setReplica Config($value)
{
$this->replica Config = $value;
$this->slave Config = $value;
}
/**
* @var array list of primary connection configurations. Each configuration is used to create a primary DB connection.
@ -434,28 +476,30 @@ class Connection extends Component
* which will be used by this object.
* Note that when this property is not empty, the connection setting (e.g. `dsn`, `username`) of this object will
* be ignored.
* @see pri mary Config
* @see shufflePrimarie s
* @since 2.0.36
* @see maste rConfig
* @see shuffleMaster s
* @deprecated since 2.0.36. Use [[primaries]] instead.
*/
public $primarie s = [];
public $master s = [];
/**
* Returns the value of [[primaries]].
* @return array
* @deprecated since 2.0.36. Use [[primaries]] instead.
* @since 2.0.36
* @internal
*/
public function getMaster s()
public function getPrimarie s()
{
return $this->primarie s;
return $this->master s;
}
/**
* Sets the value of [[primaries]].
* @param array $value
* @deprecated since 2.0.36. Use [[primaries]] instead.
* @since 2.0.36
* @internal
*/
public function setMaster s($value)
public function setPrimarie s($value)
{
$this->primarie s = $value;
$this->master s = $value;
}
/**
* @var array the configuration that should be merged with every primary configuration listed in [[primaries]].
@ -472,51 +516,55 @@ class Connection extends Component
* ]
* ```
*
* @since 2.0.36
* @deprecated since 2.0.36. Use [[primaryConfig]] instead.
*/
public $pri mary Config = [];
public $maste rConfig = [];
/**
* Returns the value of [[primaryConfig]].
* @return array
* @deprecated since 2.0.36. Use [[primaryConfig]] instead.
* @since 2.0.36
* @internal
*/
public function getMaster Config()
public function getPrimary Config()
{
return $this->pri mary Config;
return $this->maste rConfig;
}
/**
* Sets the value of [[primaryConfig]].
* @param array $value
* @deprecated since 2.0.36. Use [[primaryConfig]] instead.
* @since 2.0.36
* @internal
*/
public function setMaster Config($value)
public function setPrimary Config($value)
{
$this->pri mary Config = $value;
$this->maste rConfig = $value;
}
/**
* @var bool whether to shuffle [[primaries]] before getting one.
* @since 2.0.11
* @see primarie s
* @since 2.0.36
* @see master s
* @deprecated since 2.0.36. Use [[shufflePrimaries]] instead.
*/
public $shufflePrimarie s = true;
public $shuffleMaster s = true;
/**
* Returns the value of [[shufflePrimaries]].
* @return bool
* @deprecated since 2.0.36. Use [[shufflePrimaries]] instead.
* @since 2.0.36
* @internal
*/
public function getShuffleMaster s()
public function getShufflePrimarie s()
{
return $this->shufflePrimarie s;
return $this->shuffleMaster s;
}
/**
* Sets the value of [[shufflePrimaries]].
* @param bool $value
* @deprecated since 2.0.36. Use [[shufflePrimaries]] instead.
* @since 2.0.36
* @internal
*/
public function setShuffleMaster s($value)
public function setShufflePrimarie s($value)
{
$this->shufflePrimarie s = $value;
$this->shuffleMaster s = $value;
}
/**
* @var bool whether to enable logging of database queries. Defaults to true.
@ -714,8 +762,8 @@ class Connection extends Component
return;
}
if (!empty($this->primarie s)) {
$db = $this->getPrimary ();
if (!empty($this->master s)) {
$db = $this->getMaster ();
if ($db !== null) {
$this->pdo = $db->pdo;
return;
@ -1087,7 +1135,7 @@ class Connection extends Component
if (($pos = strpos($this->dsn, ':')) !== false) {
$this->_driverName = strtolower(substr($this->dsn, 0, $pos));
} else {
$this->_driverName = strtolower($this->getReplica Pdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
$this->_driverName = strtolower($this->getSlave Pdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
}
}
@ -1124,9 +1172,9 @@ class Connection extends Component
*/
public function getReplicaPdo($fallbackToPrimary = true)
{
$db = $this->getReplica (false);
$db = $this->getSlave (false);
if ($db === null) {
return $fallbackToPrimary ? $this->getPrimary Pdo() : null;
return $fallbackToPrimary ? $this->getMaster Pdo() : null;
}
return $db->pdo;
@ -1181,12 +1229,12 @@ class Connection extends Component
*/
public function getReplica($fallbackToPrimary = true)
{
if (!$this->enableReplica s) {
if (!$this->enableSlave s) {
return $fallbackToPrimary ? $this : null;
}
if ($this->_replica === false) {
$this->_replica = $this->openFromPool($this->replicas, $this->replica Config);
$this->_replica = $this->openFromPool($this->slaves, $this->slave Config);
}
return $this->_replica === null & & $fallbackToPrimary ? $this : $this->_replica;
@ -1218,9 +1266,9 @@ class Connection extends Component
public function getPrimary()
{
if ($this->_primary === false) {
$this->_primary = $this->shufflePrimarie s
? $this->openFromPool($this->primaries, $this->primary Config)
: $this->openFromPoolSequentially($this->primaries, $this->primary Config);
$this->_primary = $this->shuffleMaster s
? $this->openFromPool($this->masters, $this->master Config)
: $this->openFromPoolSequentially($this->masters, $this->master Config);
}
return $this->_primary;
@ -1259,19 +1307,19 @@ class Connection extends Component
*/
public function usePrimary(callable $callback)
{
if ($this->enableReplica s) {
$this->enableReplica s = false;
if ($this->enableSlave s) {
$this->enableSlave s = false;
try {
$result = call_user_func($callback, $this);
} catch (\Exception $e) {
$this->enableReplica s = true;
$this->enableSlave s = true;
throw $e;
} catch (\Throwable $e) {
$this->enableReplica s = true;
$this->enableSlave s = true;
throw $e;
}
// TODO: use "finally" keyword when miminum required PHP version is >= 5.5
$this->enableReplica s = true;
$this->enableSlave s = true;
} else {
$result = call_user_func($callback, $this);
}