MongoEngine: Close connection
Asked Answered
M

4

11

I spent ages trying to find a simple example where MongoEngine was being used and a connection was being closed. Finally figured it out and posting my code.

Menendez answered 9/5, 2017 at 9:28 Comment(0)
K
15

I know this is an old question, but if anyone else is searching I figured I'd give an alternate answer.

close() does not actually remove the connection from MongoEngine's connection list. This causes problems when trying to connect to a different database later on.

To solve this I used mongoengine.connection.disconnect (even though it's not listed in __all__). My code looks like this:

from mongoengine import connect
from mongoengine.connection import disconnect

db = connect(alias='some_alias')

{do stuff}

disconnect(alias='some_alias')

You can also leave the alias out as it will default to 'default' in both connect and disconnect.

Kulak answered 29/8, 2018 at 18:25 Comment(0)
M
8

I thought disconnect() was supposed to be used initially, but it has been removed as a synonym for close().

from mongoengine import connect

def main():

    #connect to db
    db_client = connect('my_db', host='localhost', port=27017)

    #close the connection
    db_client.close()

if __name__ == "__main__":
    main()
Menendez answered 9/5, 2017 at 9:28 Comment(2)
mongoengine maintainer here, use disconnect instead of closeAnnal
That's not true, in the documentation: The :meth:`MongoClient.disconnect` method is removed; it was a synonym for :meth:`~pymongo.MongoClient.closeClack
T
4

It can be managed with Connection class like bellow. It creates connection with __enter__ and closes it with __exit__ method.

from mongoengine import connect
from app.config import config


class Connection:
    def __enter__(self):
        self.conn = connect(host=config.mongo_url)
        return self.conn

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.conn.close()

Then you can use it with "with" statement.

from app.connection import Connection

with Connection():
     # do some stuff with db, connection will be closed after with statement
     pass 
Tenace answered 12/12, 2018 at 18:28 Comment(0)
G
0

According to the mongoengine docs

Calling disconnect() without argument will disconnect the “default” connection

As pointed out in accepted answer, in some cases it is important to definine "alias" when using connect and disconnect.

Experiments without defining "alias"

In my case connecting with alias='testdb' and disconnecting without defining 'alias' worked well until I moved my db and backend inside docker. For some reason, when running tests with mongomock inside docker I got the following errors:

mongoengine.connection.ConnectionFailure: A different connection with alias `testdb` was already registered. Use disconnect() first

and

mongoengine.connection.ConnectionFailure: You have not defined a default connection

Solution

After defining alias='testdb' also when disconnecting, everything worked well

Grimmett answered 13/1, 2021 at 10:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.