Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 query, 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)

like image 765
fancy Avatar asked Feb 15 '26 06:02

fancy


1 Answers

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.

like image 126
Andz Avatar answered Feb 16 '26 20:02

Andz