Flash
Flash

Reputation: 16703

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.

Upvotes: 8

Views: 2860

Answers (2)

Rob
Rob

Reputation: 11

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

Upvotes: 1

bbbonthemoon
bbbonthemoon

Reputation: 1778

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.

Upvotes: 4

Related Questions