mongodb3.x java driver details

   Recently, the business needs to use mongo in the child thread. I thought that the run method in the thread ends, and the MongoClient will be automatically destroyed. In the actual test, it is found that after the execution of the run method statement of the sub-thread, if the MongoClient is not closed, the sub-thread will always exist and will not be destroyed.

   At first, I thought that this would only happen in the sub-thread. Later, the MongoClient is directly initialized in the main method. If it is not closed, all the statements of the main thread will be executed, and the main thread will not stop, but will always exist. I haven't encountered this problem before, and it may only be a recent 3.x driver.

    Therefore, when we often use the singleton MongoClient, there is no problem, because all resources will be released when the process ends. Of course, the resources that have always existed in MongoClient in some way will also be released. However, if the MongoClient is initialized in the child thread without closing it, it will cause memory leaks. Remember that the MongoClient opened by itself in the child thread should call the close method in time to ensure that the resources of the MongoClient are released.

   mongo java driver versionmongo-java-driver-3.2.0

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326950182&siteId=291194637