I have a problem regarding to the Mongo client connections. I want to understand why is mongo opening new connections for the same client, the same query. I’m currently using PHP 5.6 with Mongo 3.6.17 and the mongo php driver is GitHub - mongodb/mongo-php-driver-legacy: Legacy MongoDB PHP driver.
This is the Connect function:
function Connect ($dbName, $dbURI){
if (!empty($this->connection)) {
return;
} else {
$options = [
'connect' => true,
'connectTimeoutMS' => 10000,
];
$mongo = new MongoClient(
$dbURI,
$options
);
$this -> connection = $mongo -> $dbName;
Note: Every time reloads the page that uses the MongoClient is opening a new connection, which is not what I want. I’d like to keep the same connection or when the user leaves the page Mongo can close that. Otherwise, I’ll end up with twice the number of connections from a single client. The following image demonstrates that 3161 connections opened, but having around 1000 users in the application doesn’t make sense for me.