Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mongo DB Initial connection pool remains open?

Mongo DB Initial connection pool remains open?

Problem

Currently working with Mongodb using the node.js (Native MongoDB) driver, I know it is possible to set the pooled connection count but regardless the same behaviour is observed.

The code in question:

(function init(){
    db.connect("mongodb://localhost/test",function(err, database){

    });
})();

This is the log from the server:

Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58663 #6
(1 connection now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58664 #7
(2 connections now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58665 #8
(3 connections now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58666 #9
(4 connections now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58667 #10
 (5 connections now open)
Thu Dec 06 20:19:36 [conn9] end connection 127.0.0.1:58666 (4 connections now op
en)
Thu Dec 06 20:19:36 [conn10] end connection 127.0.0.1:58667 (4 connections now o
pen)
Thu Dec 06 20:19:36 [conn8] end connection 127.0.0.1:58665 (4 connections now op
en)
Thu Dec 06 20:19:36 [conn6] end connection 127.0.0.1:58663 (4 connections now op
en)
Thu Dec 06 20:19:36 [conn7] end connection 127.0.0.1:58664 (4 connections now op
en)

The connections are apparently closing yet the current open connections count doesn't decrement even though the connections are correctly incrementing. Intended behaviour?

PS. Please don't answer with use a nodejs wrapper for mongodb.

Problem courtesy of: f0x

Solution

It's problem with MongoDB counter. Database is working asynchronously on closing all your connections (multiple close signals) and it happens so fast that it cannot keep up with updating counter. After browsing MongoDB source code you can see that message output is nowhere near counter update.

Below similar output however with up to 1ms precision which illustrates problem IMO a bit better.

Mon Dec  2 17:42:09.059 [conn675] end connection 127.0.0.1:65198 (9 connections now open)
Mon Dec  2 17:42:09.059 [conn676] end connection 127.0.0.1:65199 (8 connections now open)
Mon Dec  2 17:42:09.059 [conn677] end connection 127.0.0.1:65200 (8 connections now open)
Mon Dec  2 17:42:09.059 [conn678] end connection 127.0.0.1:65201 (7 connections now open)
Mon Dec  2 17:42:09.059 [conn679] end connection 127.0.0.1:65202 (6 connections now open)
Mon Dec  2 17:42:09.059 [conn680] end connection 127.0.0.1:65203 (5 connections now open)
Mon Dec  2 17:42:09.059 [conn681] end connection 127.0.0.1:65204 (4 connections now open)
Mon Dec  2 17:42:09.059 [conn682] end connection 127.0.0.1:65205 (3 connections now open)
Mon Dec  2 17:42:09.059 [conn683] end connection 127.0.0.1:65206 (2 connections now open)
Mon Dec  2 17:42:09.059 [conn684] end connection 127.0.0.1:65207 (2 connections now open)
Solution courtesy of: XLII

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Mongo DB Initial connection pool remains open?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×