How should the lifecycle of MongoClient work?
You are using MongoClient
incorrectly. As indicated in the docs
It is recommended to store a
MongoClient
instance in a global place, either as a static variable or in an IoC container with a singleton lifetime.
So you should rework your code to create a singleton MongoClient
instance and then use that throughout your program.
The reason is that MongoClient
maintains a pool of open connections that can be shared and reused for best performance.