Friday, March 3, 2017

Mongoose close connection

Once the connection is created by mongoose, it will create a pool of connections. The pool is maintained by the driver so that the connections can be re-used. The best practice is to create a new connection only once for the whole application.

In MongoDB console, use this command to check the connections opened
The result is
{ "current" : 2, "available" : 999998, "totalCreated" : 14 }
One connection is by the mongoDB console, the other one is nodejs.

The interesting thing is that the number is not increased when I use JMeter to generate 10 concurrent requests.

We shall close the connection before exiting the nodejs program.

After the nodejs program exists, check the connection again and now it is
{ "current" : 1, "available" : 999998, "totalCreated" : 14 }

Here are the helpful articles.
http://stackoverflow.com/questions/23244843/how-mongodb-connection-works-on-concurrent-requests-in-nodejs-express-server

https://gist.github.com/pasupulaphani/9463004#file-mongoose_connet-js

find which command connect to mongodb
http://stackoverflow.com/questions/8975531/check-the-current-number-of-connections-to-mongodb
in windows, use this command to get pid
then use this command to find the process, replace 6888 with the pid found in the previous command

No comments:

Post a Comment