I am using flask
and pymongo
for a small project and I'm not experienced in flask
. In Mongodb, closing connection and making a new one every time is not recommended. If I put my database connection in app context, as the documentation says for SQL database, it gets renewed in every request. Is there a way I can make the connection once and reuse it every time except for g
, which appears to be in app context? Is my perception wrong?
Asked
Active
Viewed 165 times
1
-
1`The client object is thread-safe and has connection-pooling built in. If an operation fails because of a network error, ConnectionFailure is raised and the client reconnects in the background. Application code should handle this exception (recognizing that the operation failed) and then continue to execute.` Quote from the pymongo doc, could you link the renewing thing? – PolishCivil Feb 12 '19 at 05:24
-
https://stackoverflow.com/questions/14495975/why-is-it-recommended-not-to-close-a-mongodb-connection-anywhere-in-node-js-code please see the answer here, I can't find the doc right now, but its there. – Al Amin Feb 12 '19 at 05:29
-
1Well, the thing is that they `do` use the connection polling. You open the mongodb client thing once per lifecycle of your app, the pymongo handles disconnections, connections internally. So i'd say keep it in the app context or inject the reference to the client everywhere you use it, do not create multiple pymongo clients – PolishCivil Feb 12 '19 at 05:34