In a real server situation,
do we really need close mongoose
in a deployed Node.js server?
My idea is something like this:
mongoose.connect(url, { keepAlive: 120 }).then(
() => {
// We are doing something exciting here...
// After we've done.
return mongoose.connection.close();
},
err => {
console.error(err);
}
);
I know this work, but is it anti-pattern if we want to keepAlive
and also close the connection?
The article about why I want to keep it alive is found here: http://mongoosejs.com/docs/connections.html#keepAlive
This question was asked similar thing but it's for an older version of mongoose:
Do I need to manually close a mongoose connection?
Note:
I'm using "mongoose": "^5.0.3"
, the supported functions have changed from 4.
In mongoose 5.0.3 documentation, there is nowhere to find it requires to close connection, and the official example also doesn't include it.