From fac968a4632d3b61a3218cbdf5dbedf5818ba107 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 7 Nov 2024 12:58:59 +0100 Subject: [PATCH] Disable mongoc_client reuse between connections --- src/Connection.php | 4 ++++ tests/ConnectionTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Connection.php b/src/Connection.php index 84ca97aba..592e500e5 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -217,6 +217,10 @@ protected function createConnection(string $dsn, array $config, array $options): $options['password'] = $config['password']; } + if (isset($config['name'])) { + $driverOptions += ['connectionName' => $config['name']]; + } + return new Client($dsn, $options, $driverOptions); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index fe3272943..affb6bd8a 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -277,6 +277,34 @@ public function testQueryLog() } } + public function testQueryLogWithMultipleClients() + { + $connection = DB::connection('mongodb'); + $this->assertInstanceOf(Connection::class, $connection); + + // Create a second connection with the same config as the first + // Make sure to change the name as it's used as a connection identifier + $config = $connection->getConfig(); + $config['name'] = 'mongodb2'; + $secondConnection = new Connection($config); + + $connection->enableQueryLog(); + $secondConnection->enableQueryLog(); + + $this->assertCount(0, $connection->getQueryLog()); + $this->assertCount(0, $secondConnection->getQueryLog()); + + $connection->table('items')->get(); + + $this->assertCount(1, $connection->getQueryLog()); + $this->assertCount(0, $secondConnection->getQueryLog()); + + $secondConnection->table('items')->get(); + + $this->assertCount(1, $connection->getQueryLog()); + $this->assertCount(1, $secondConnection->getQueryLog()); + } + public function testDisableQueryLog() { // Disabled by default