Skip to content

Commit 0d22ee4

Browse files
authored
Ability to disable auto slave select for all read queries (#289)
1 parent f4eeb85 commit 0d22ee4

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Command/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function prepare(?bool $forRead = null): void
280280

281281
$sql = $this->getSql();
282282

283-
if ($this->db->getTransaction()) {
283+
if ($this->db->getTransaction() || !$this->db->isAutoSlaveForReadQueriesEnabled()) {
284284
/** master is in a transaction. use the same connection. */
285285
$forRead = false;
286286
}

src/Connection/Connection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ abstract class Connection implements ConnectionInterface
176176
private bool $enableSavepoint = true;
177177
private int $serverRetryInterval = 600;
178178
private bool $enableSlaves = true;
179+
private bool $enableAutoSlaveForReadQueries = true;
179180
private array $slaves = [];
180181
private array $masters = [];
181182
private bool $shuffleMasters = true;
@@ -372,6 +373,11 @@ public function areSlavesEnabled(): bool
372373
return $this->enableSlaves;
373374
}
374375

376+
public function isAutoSlaveForReadQueriesEnabled(): bool
377+
{
378+
return $this->enableAutoSlaveForReadQueries;
379+
}
380+
375381
/**
376382
* Returns a value indicating whether the DB connection is established.
377383
*
@@ -888,6 +894,11 @@ public function setEnableSlaves(bool $value): void
888894
$this->enableSlaves = $value;
889895
}
890896

897+
public function setEnableAutoSlaveForReadQueries(bool $value): void
898+
{
899+
$this->enableAutoSlaveForReadQueries = $value;
900+
}
901+
891902
/**
892903
* Set connection for master server, you can specify multiple connections, adding the id for each one.
893904
*

src/Connection/ConnectionInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public function getServerVersion(): string;
6969
*/
7070
public function getTableSchema(string $name, $refresh = false): ?TableSchema;
7171

72+
/**
73+
* Whether to enable auto recogintion of read queries and use slave (if enabled) for execute.
74+
*
75+
* @return bool For default `true` use slave for read queries, `false` use master connection (for reads and writes).
76+
* Slave still can be used via $this->getSlave().
77+
*/
78+
public function isAutoSlaveForReadQueriesEnabled(): bool;
79+
7280
/**
7381
* Whether to enable read/write splitting by using {@see setSlaves()} to read data. Note that if {@see setSlaves()}
7482
* is empty, read/write splitting will NOT be enabled no matter what value this property takes.

src/TestUtility/TestConnectionTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,13 @@ public function testClone(): void
384384
$this->assertNull($conn3->getTransaction());
385385
$this->assertNull($conn3->getPDO());
386386
}
387+
388+
public function testDisableAutoSlaveForReads(): void
389+
{
390+
$db = $this->getConnection();
391+
$this->assertTrue($db->isAutoSlaveForReadQueriesEnabled());
392+
393+
$db->setEnableAutoSlaveForReadQueries(false);
394+
$this->assertFalse($db->isAutoSlaveForReadQueriesEnabled());
395+
}
387396
}

0 commit comments

Comments
 (0)