Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: Unshared DB instance affects subsequent shared DB instances
### PHP Version

8.4

### CodeIgniter4 Version

develop, 4.7

### CodeIgniter4 Installation Method

Git

### Which operating systems have you tested for this bug?

macOS

### Which server did you use?

cli

### Database

SQLite3

### What happened?

When you retrieve a non-shared DB instance via `Database::connect(getShared: false)` and then modifying that instance, all subsequent shared instances would use that non-shared instance if given the same DB group.

### Steps to Reproduce

```php
<?php

namespace App\Controllers;

use CodeIgniter\Test\ReflectionHelper;
use Config\Database;

class Home extends BaseController
{
    use ReflectionHelper;

    public function index(): string
    {
        $sharedDb1 = Database::connect('tests');

        $unsharedDb = Database::connect('tests', false);
        self::setPrivateProperty($unsharedDb, 'DBDebug', false);

        $sharedDb2 = Database::connect('tests');

        return sprintf(
            '<pre>
                <table border="1" cellpadding="4" cellspacing="0">
                    <tr>
                        <th>Connection</th>
                        <th>Debug Type</th>
                    </tr>
                    <tr>
                        <td>Shared DB 1</td>
                        <td>%s</td>
                    </tr>
                    <tr>
                        <td>Unshared DB</td>
                        <td>%s</td>
                    </tr>
                    <tr>
                        <td>Shared DB 2</td>
                        <td>%s</td>
                    </tr>
                </table>',
            var_export(self::getPrivateProperty($sharedDb1, 'DBDebug'), true),
            var_export(self::getPrivateProperty($unsharedDb, 'DBDebug'), true),
            var_export(self::getPrivateProperty($sharedDb2, 'DBDebug'), true),
        );
    }
}

```

Running `php spark serve` produces:
<pre>
    <table border="1" cellpadding="4" cellspacing="0">
        <tr>
            <th>Connection</th>
            <th>Debug Type</th>
        </tr>
        <tr>
            <td>Shared DB 1</td>
            <td>true</td>
        </tr>
        <tr>
            <td>Unshared DB</td>
            <td>false</td>
        </tr>
        <tr>
            <td>Shared DB 2</td>
            <td>false</td>
        </tr>
    </table>
</pre>

### Expected Output

I expect `$sharedDb1` and `$sharedDb2` to have the same DBDebug value as they are "shared".
<pre>
    <table border="1" cellpadding="4" cellspacing="0">
        <tr>
            <th>Connection</th>
            <th>Debug Type</th>
        </tr>
        <tr>
            <td>Shared DB 1</td>
            <td>true</td>
        </tr>
        <tr>
            <td>Unshared DB</td>
            <td>false</td>
        </tr>
        <tr>
            <td>Shared DB 2</td>
            <td>true</td>
        </tr>
    </table>
</pre>

### Anything else?

_No response_



Fix (patch):
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/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 <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
 for a complete list of bugs fixed.