JF0001
JF0001

Reputation: 839

Is Closing Mongo DB Connections for Web App Necessary

I have a web app which uses a Mongo Database in the backend. Upon starting the Web Server, a connection to the DB is created using MongoClient.connect. From my front-end app I then make numerous axios.post calls to the DB, some of which return a single row, while others return cursors with several rows. Do I need to close some connections to the database at any point in time, either directly on the backend or from the front-end?

Upvotes: 0

Views: 74

Answers (1)

Tobias S.
Tobias S.

Reputation: 23825

You do not need to close your connection anytime when your backend is running. It is good practice to close the connection when your backend process is shutting down though.

process.on('SIGINT', function() {
  mongoclient.close()
});

Upvotes: 1

Related Questions