more robust check added to mongo db connection

Calling mongoengine (or pymongo) connect() after already calling
connect() resulting in getting the first connection (connection
pooling). This is bad if we are relying on connect() to determine if
mongo is up. Added executing a ping/pong command after the connect()
command to ensure mongo is still/really up.
This commit is contained in:
Chris Meyers 2015-12-04 13:23:43 -05:00
parent 58be1b1f51
commit 78ac2ccb55

View File

@ -4,6 +4,7 @@
from django.conf import settings
from mongoengine import connect
from mongoengine.connection import ConnectionError
from pymongo.errors import AutoReconnect
def test_mongo_connection():
# Connect to Mongo
@ -14,13 +15,14 @@ def test_mongo_connection():
raise ConnectionError
# Attempt to connect to the MongoDB database.
connect(settings.MONGO_DB,
host=settings.MONGO_HOST,
port=int(settings.MONGO_PORT),
username=settings.MONGO_USERNAME,
password=settings.MONGO_PASSWORD,
tz_aware=settings.USE_TZ)
db = connect(settings.MONGO_DB,
host=settings.MONGO_HOST,
port=int(settings.MONGO_PORT),
username=settings.MONGO_USERNAME,
password=settings.MONGO_PASSWORD,
tz_aware=settings.USE_TZ)
db[settings.MONGO_DB].command('ping')
return True
except ConnectionError:
except ConnectionError, AutoReconnect:
return False