Reputation: 3380
I read this article on connection pooling with mongodb/nodejs. There he opens the connection once, and leaves it at that.
This is how I set up the database connection in my app.js
file:
mongodb.MongoClient.connect(MONGODB_URI, function (error, database) {
if (error) throw error;
db = database; // db is defined outside this callback
coll = db.collection('testData'); // coll is defined outside this callback
});
This is going to leave the db connection open as long as the server is running. Shouldn't you close the connection at some point? Or is it of no consequence to leave it open?
Upvotes: 4
Views: 1062
Reputation: 312129
If your app has support for controlled shutdown, then you should close the connection pool at that time. Otherwise you just leave it open.
The connection pool manages the number of actual connections for you, adding more in times of heavy load and closing them when your app is idle.
Upvotes: 3