mongokit icon indicating copy to clipboard operation
mongokit copied to clipboard

How to close connections?

Open sushuai opened this issue 10 years ago • 1 comments

I can not find the method closed conntions,when I run program, the conntions growing fast until mongodb conntions full and then I can not connect to mongodb, how can I close connections?

sushuai avatar Dec 31 '15 03:12 sushuai

@sushuai It's simple to close connections opened by MongoKit. Here are my solution:

# create a connection
connection = Connection()
# do something
...
# close the connection
connection.close()

And even more, if you use MongoKit in a Flask-RESTful project, it's also very simple to close connections opened by each API request:

from flask import Flask
from flask_restful import Api


app = Flask(__name__)
api = Api(app)

@app.teardown_request
def teardown_request(exception):  # close db connection after each api request
    connection.close()

ferstar avatar Mar 01 '16 03:03 ferstar