MongoDB: connection already opened error
I'm unsure if I'm calling the database, mongo, efficiently. Right now I, open the db, get the collection, do my q开发者_JAVA技巧uery, close the connection.
I', having a problem with mongo sporadically throwing: connection already opened
EDIT: All I'm doing is logging in and out a few times quickly and this error gets thrown, crashing the server.
What would cause this issue?
Thanks very much for any help!
(I'm using the node-native driver)
Since node.js is asynchronous, you can't be certain that your code will execute completely in one go.
It could be that during an asynchronous operation (as all the db ops are), node.js is handling another request, which will open up a new connection even though you already have one open.
What you should do is open up a single connection for the entire app. It is too inefficient to connect to the database at every page request, and as you have experienced, it can also cause problems if you don't implement it properly.
精彩评论