Do I need to manually close a mongoose connection?

前端 未结 3 1382
别那么骄傲
别那么骄傲 2020-12-15 20:20

New to Node, Mongoose & Mongodb - haven\'t read the source code...

I have a Node application which opens a file, parses the lines into records and saves the reco

3条回答
  •  我在风中等你
    2020-12-15 20:48

    You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown.

    Another possible scenario is to close a connection when a data streaming is done. Anyway is more recommended to connect on startup and disconnect on shutdown.

    This is the code for disconnection on a SIGINT signal.

    // If the Node process ends, close the Mongoose connection
    process.on('SIGINT', function() {
      mongoose.connection.close(function () {
        console.log('Mongoose disconnected on app termination');
        process.exit(0);
      });
    });
    

提交回复
热议问题