def get_conn():
"""
Get a database connection
Ensures that only one global database connection exists per thread.
If the connection does not exist a new one is created and returned.
"""
if external_client is not None:
return external_client
global __client, __connection
if not __connection:
try:
__client = MongoClient(mongo_addr, mongo_port)
__connection = __client[mongo_db_name]
except ConnectionFailure:
raise SevereInternalException("Could not connect to mongo database {} at {}:{}".format(mongo_db_name, mongo_addr, mongo_port))
except InvalidName as error:
raise SevereInternalException("Database {} is invalid! - {}".format(mongo_db_name, error))
return __connection
评论列表
文章目录