Is it possible to connect MongoDB to a timeout in Python?

(newbie question, sorry for that - I'm just starting with MongoDB)

I connect to mongo on heroku as follows:

self.connection = pymongo.Connection(MONGO_URL) self.db = self.connection.app13805318 

Is it possible that I will try to use self.db after a few hours and cannot read it? Do I need to do any monitoring or updating the connection?

+4
source share
1 answer

Quoted from Pymongo documentation: http://api.mongodb.org/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient

As a result, the client object has an integrated connection pool. It also automatically reconnects. If the operation failed due to a connection error, the ConnectionFailure will be raised. If automatic reconnection is performed, AutoReconnect will be activated. Application code must handle this exception (recognizing that the operation failed), and then continue execution.

Since @ james-wahlin is suggested in the comments, you should not rely solely on the pymongo connection pool mechanism, but always wrap using self.db in try..except sentences.

Hope this helps.

+4
source

Source: https://habr.com/ru/post/1491904/


All Articles