Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close() never close connections in pymongo?

I use MongoDB and I connect to it through pymongo. Here's my code:

>>> import pymongo
>>> con=pymongo.Connection('localhost',27017)
>>> con.database_names()
['local', 'bookdb']
>>> con.close()
>>> con.database_names()
['local', 'bookdb']

I use con.close() to disconnect to the MongoDB, but after that, I can still use con.database_names() to see the list of the databases. Why? it never disconnect to the MongoDB server. Why the close() not work?

like image 952
socket Avatar asked Dec 16 '13 14:12

socket


People also ask

Do I need to close PyMongo connection?

There's no need to close a Connection instance, it will clean up after itself when Python garbage collects it. You should use MongoClient instead of Connection ; Connection is deprecated. To take advantage of connection pooling, you could create one MongoClient that lasts for the entire life of your process.

How do I close a MongoDB connection PyMongo?

End all server sessions created by this client by sending one or more endSessions commands. Close all sockets in the connection pools and stop the monitor threads. .. versionchanged:: 4.0 Once closed, the client cannot be used again and any attempt will raise :exc:`~pymongo. errors.

Do I need to close MongoClient?

No, you do not need to close connections to DB - your only connection is via MongoClient and as the documentation states - it handles connection pooling for you. The only resource that you would want to clean up would be a cursor which you should close() when you're done with it.

How do I close all connections in MongoDB?

open(function (err, db) { db. close(); }); // Open another connection db. open(function (err, db) { db. close(); });


1 Answers

Just read the docs, faster and more detailed.

If this instance is used again it will be automatically re-opened.

Link to docs

like image 147
iMom0 Avatar answered Nov 08 '22 13:11

iMom0