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.
"""
global __client, __connection
if not __connection:
try:
# Allow more complex mongodb connections
conf = api.app.app.config
if conf["MONGO_USER"] and conf["MONGO_PW"]:
uri = "mongodb://{}:{}@{}:{}/{}?authMechanism=SCRAM-SHA-1".format(
conf["MONGO_USER"],
conf["MONGO_PW"],
conf["MONGO_ADDR"],
conf["MONGO_PORT"],
conf["MONGO_DB_NAME"])
else:
uri = "mongodb://{}:{}/{}".format(
conf["MONGO_ADDR"],
conf["MONGO_PORT"],
conf["MONGO_DB_NAME"])
__client = MongoClient(uri)
__connection = __client[conf["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
评论列表
文章目录