Is it necessary to close Mongodb connections with Pymongo?

Asked

Viewed 624 times

3

I am working on a REST API with Python (Flask + Mongodb) and during development I checked that, nowhere in the code, had I released the resources from the database.

I have researched around but found nothing feasible about the best way to work this approach.

I don’t know if Mongodb is self-manageable about it, or if I should programmatically manipulate it. Usually in Java or any other language, making use of drivers for Sgdbs or Orms, one has to worry about closing connections with the database when it is no longer being requested by the application.

For example, my class that provides the connection method is like this:

from mongoengine import connect
from siava.app.aplicacao.utils.Configuracoes import Configuracoes


class Conexao(object):
    """
    Mantem conexões com o banco de dados
    """

    @classmethod
    def conectar(self):
        config = Configuracoes()

        connect(
            name=config.banco['banco'],
            host=config.banco['host'],
            port=config.banco['porta'],
            username=config.banco['usuario'],
            password=config.banco['senha'])

The mongoengine (that encapsulates the Pymongo) even has the Disconnect() method but in the documentation has nothing about the use of it. It may be in-house and not part of the resource usage interface/api.

With this, the doubt remains, I must control the release of bank resources or somehow this is managed by Mongodb itself, or even by the ODM?

1 answer

1


A scalable WEB application should not open a connection to fulfill each request - instead, the practice is to have a set (Poll) of active connections that are reused to each web request.

As you are using Flask - which brings together a number of approaches related to scalable applications, it has the resources to do this, and provide for the code that runs in your views (including the code called by them), a connection instance of that Poll.

Only if you create your connection "manually" as you are doing - following the instructions only on the Mongo side, without worrying about the life cycle of a Flask request, nor about its architecture, you are not taking advantage of these mechanisms.

So, not only can your connection "leak" by staying open without being closed (it is possible that it will be closed, if you end all references to it at the end of a request), but above all, most likely you are using a lot of resources (CPU, network, time) to create a new connection for each Web request. You haven’t put in the code you use to call the connection creation - you may even be trying to recycle connections between requests - in which case you’d be on the right track, but it’s still a matter of reinventing the wheel.

Having said all this, the right way to do it is to take some package that already integrates Mongodb with Flask, taking advantage of all of Flask’s mechanisms for using a connection pod. For example, the flask-pymongo, or the very Sqlalchemy, that has some support for Mongodb as well.

  • I understand, vlw by the tips. I will try to give a studied on this issue on the treatment of connections on the Flask side. I use mongoengine as ODM for the document abstraction and persistence layer, Flask is using only as FW REST because in Hug (http://www.hug.rest/) I had some problems with POST capture.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.