Should the MongoClient connection be closed every time the server shuts down?
I have seen the following code snippet and wanted to know if this is actually valid and should be done or if it's completely unnecessary to perform a closing on exit:
// Adding listeners
function setupListeners(client: MongoClient){
client.addListener('topologyClosed', ()=>{
isTopologyConnected = false;
console.warn("topologyClosed");
})
}
process.on("exit", () => {
console.log("EXIT - MongoDB Client disconnected");
closeConnection()
});
//Cleanups
//catching signals and doing cleanup
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function (signal) {
process.on(signal, function () {
if (isTopologyConnected){
client.close();
}
process.exit(1);
});
});
Thanks a lot.