Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection pooling in node-mongodb-native, when to call db.open and db.close

I've read most questions here about node-mongodb-native but I can't work out the standard practice as to when I should open/close a connection.

Some sources say open/close as needed, some say use one db instance throughout. Does node-mongodb-native support automatic connection pooling? If so, how do I use this?

I would really appreciate example code showing correct use of db.open and db.close in relation to, say, a login request.

like image 475
Flash Avatar asked Jun 16 '12 01:06

Flash


2 Answers

I suggest to use generic-pool

It's very clear and pretty straightforward, you define how to open connection, how to close, and size of the pool. The module takes care of the rest, creating new connections as needed, and disposing unused connection after timeout you also select.

I use the module with every resource I need to pool, so I dont have to bother with custom pooling API every time.

like image 97
bbbonthemoon Avatar answered Nov 15 '22 03:11

bbbonthemoon


This is the best answer I could find. Apparently, it works automatically, but I'm still figuring out the details.

Let me know if you find anything!

http://technosophos.com/node/255

like image 43
Rob Avatar answered Nov 15 '22 03:11

Rob