File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments