Skip to content

Commit

Permalink
Disable mongoc_client reuse between connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Nov 7, 2024
1 parent c0d2635 commit fac968a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fac968a

Please sign in to comment.