diff --git a/system/Database/Config.php b/system/Database/Config.php index 6f25ad3c0059..fcf700ee3bcb 100644 --- a/system/Database/Config.php +++ b/system/Database/Config.php @@ -81,7 +81,9 @@ public static function connect($group = null, bool $getShared = true) $connection = static::$factory->load($config, $group); - static::$instances[$group] = $connection; + if ($getShared) { + static::$instances[$group] = $connection; + } return $connection; } diff --git a/tests/system/Database/Live/ConnectTest.php b/tests/system/Database/Live/ConnectTest.php index 3f7941a201ec..e41fdbdfc114 100644 --- a/tests/system/Database/Live/ConnectTest.php +++ b/tests/system/Database/Live/ConnectTest.php @@ -115,4 +115,22 @@ public function testConnectWithFailover(): void $this->assertGreaterThanOrEqual(0, count($db1->listTables())); } + + public function testNonSharedInstanceDoesNotAffectSharedInstances(): void + { + $firstSharedDb = Database::connect('tests'); + $originalDebugValue = (bool) self::getPrivateProperty($firstSharedDb, 'DBDebug'); + + $nonSharedDb = Database::connect('tests', false); + self::setPrivateProperty($nonSharedDb, 'DBDebug', ! $originalDebugValue); + + $secondSharedDb = Database::connect('tests'); + + $this->assertSame($firstSharedDb, $secondSharedDb); + $this->assertNotSame($firstSharedDb, $nonSharedDb); + + $this->assertSame($originalDebugValue, self::getPrivateProperty($firstSharedDb, 'DBDebug')); + $this->assertSame($originalDebugValue, self::getPrivateProperty($secondSharedDb, 'DBDebug')); + $this->assertSame(! $originalDebugValue, self::getPrivateProperty($nonSharedDb, 'DBDebug')); + } } diff --git a/user_guide_src/source/changelogs/v4.6.4.rst b/user_guide_src/source/changelogs/v4.6.4.rst index 7cae67431354..8ef80eabbfcf 100644 --- a/user_guide_src/source/changelogs/v4.6.4.rst +++ b/user_guide_src/source/changelogs/v4.6.4.rst @@ -30,6 +30,8 @@ Deprecations Bugs Fixed ********** +- **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache. + See the repo's `CHANGELOG.md `_ for a complete list of bugs fixed.