Skip to content

Commit 103f1a9

Browse files
fix: prevent non-shared DB instances from polluting shared cache (#9679)
* fix: prevent non-shared DB instances from polluting shared cache * Update user_guide_src/source/changelogs/v4.6.4.rst Co-authored-by: John Paul E. Balandan, CPA <[email protected]> --------- Co-authored-by: John Paul E. Balandan, CPA <[email protected]>
1 parent 509ed87 commit 103f1a9

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

system/Database/Config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public static function connect($group = null, bool $getShared = true)
8181

8282
$connection = static::$factory->load($config, $group);
8383

84-
static::$instances[$group] = $connection;
84+
if ($getShared) {
85+
static::$instances[$group] = $connection;
86+
}
8587

8688
return $connection;
8789
}

tests/system/Database/Live/ConnectTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,22 @@ public function testConnectWithFailover(): void
115115

116116
$this->assertGreaterThanOrEqual(0, count($db1->listTables()));
117117
}
118+
119+
public function testNonSharedInstanceDoesNotAffectSharedInstances(): void
120+
{
121+
$firstSharedDb = Database::connect('tests');
122+
$originalDebugValue = (bool) self::getPrivateProperty($firstSharedDb, 'DBDebug');
123+
124+
$nonSharedDb = Database::connect('tests', false);
125+
self::setPrivateProperty($nonSharedDb, 'DBDebug', ! $originalDebugValue);
126+
127+
$secondSharedDb = Database::connect('tests');
128+
129+
$this->assertSame($firstSharedDb, $secondSharedDb);
130+
$this->assertNotSame($firstSharedDb, $nonSharedDb);
131+
132+
$this->assertSame($originalDebugValue, self::getPrivateProperty($firstSharedDb, 'DBDebug'));
133+
$this->assertSame($originalDebugValue, self::getPrivateProperty($secondSharedDb, 'DBDebug'));
134+
$this->assertSame(! $originalDebugValue, self::getPrivateProperty($nonSharedDb, 'DBDebug'));
135+
}
118136
}

user_guide_src/source/changelogs/v4.6.4.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache.
34+
3335
See the repo's
3436
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3537
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)