Develop MongoDB connection Be sure to close the connection, the exception must also be off

Source: Internet
Author: User
Tags mongodb driver mongodb server

Waste a lot of time to store data, all migrated to MongoDB, not a large number of changes is the main cause.

But there was a strange phenomenon.

Soon after the program ran, MongoDB is suspended animation, and another client to login to MongoDB is not.

Restarting the MONGODB server is not getting better.

The reason for this analysis is that MongoDB's connection pool is full and not released.

But from the official website to understand the MongoDB connection is self-management, after the use of automatic release, write a few demo test, also fully meet this point.

Wasted a lot of time looking for the cause of the problem and trying to optimize it.

For example, set the number of TCP connections for Windows, and so on.

Set the number of MONGODB connections, wait time, and so on.

But it's not going to work out.

Finally, I thought, MongoDB connection is self-managed, has its own connection pool, but mongdb with the program I developed before, there is a third party that encapsulates the MongoDB driver.

Think about what is going on inside the driver, so that the connection is maintained for a long time and cannot be returned?

With a try mentality, rewrite all the project code, try{}catch{} in the catch manually close to release the connection.

As a result, the problem no longer occurs.

The reason is clear, because of the anomaly.

MongoDB Connection Smooth completion of the task, automatic release, but with the driver, if out of the ordinary, do not actively switch off the connection, even after the program runs for a period of time, the connection pool is overdrawn.

Develop MongoDB connection Be sure to close the connection, the exception must also be off

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.