mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 14:35:45 +00:00
improved mongodb connection handling
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
bef98a8410
commit
18ca4c17e3
@ -44,7 +44,8 @@ class LocalMongoDBConnection(DBConnection):
|
|||||||
self.crlfile = _kwargs_parser(key="crlfile", kwargs=kwargs) or dbconf['crlfile']
|
self.crlfile = _kwargs_parser(key="crlfile", kwargs=kwargs) or dbconf['crlfile']
|
||||||
self.max_tries = _kwargs_parser(key="max_tries", kwargs=kwargs)
|
self.max_tries = _kwargs_parser(key="max_tries", kwargs=kwargs)
|
||||||
self.connection_timeout = _kwargs_parser(key="connection_timeout", kwargs=kwargs)
|
self.connection_timeout = _kwargs_parser(key="connection_timeout", kwargs=kwargs)
|
||||||
self.conn = self.connect()
|
self.__conn = None
|
||||||
|
self.connect()
|
||||||
|
|
||||||
if not self.ssl:
|
if not self.ssl:
|
||||||
self.ssl = False
|
self.ssl = False
|
||||||
@ -53,7 +54,7 @@ class LocalMongoDBConnection(DBConnection):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def db(self):
|
def db(self):
|
||||||
return self.conn[self.dbname]
|
return self.connect()[self.dbname]
|
||||||
|
|
||||||
def query(self):
|
def query(self):
|
||||||
return Lazy()
|
return Lazy()
|
||||||
@ -69,11 +70,11 @@ class LocalMongoDBConnection(DBConnection):
|
|||||||
def run(self, query):
|
def run(self, query):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return query.run(self.conn)
|
return query.run(self.connect())
|
||||||
except pymongo.errors.AutoReconnect:
|
except pymongo.errors.AutoReconnect:
|
||||||
logger.warning('Lost connection to the database, '
|
logger.warning('Lost connection to the database, '
|
||||||
'retrying query.')
|
'retrying query.')
|
||||||
return query.run(self.conn)
|
return query.run(self.connect())
|
||||||
except pymongo.errors.AutoReconnect as exc:
|
except pymongo.errors.AutoReconnect as exc:
|
||||||
raise ConnectionError from exc
|
raise ConnectionError from exc
|
||||||
except pymongo.errors.DuplicateKeyError as exc:
|
except pymongo.errors.DuplicateKeyError as exc:
|
||||||
@ -93,7 +94,8 @@ class LocalMongoDBConnection(DBConnection):
|
|||||||
:exc:`~ConfigurationError`: If there is a ConfigurationError while
|
:exc:`~ConfigurationError`: If there is a ConfigurationError while
|
||||||
connecting to the database.
|
connecting to the database.
|
||||||
"""
|
"""
|
||||||
|
if self.__conn:
|
||||||
|
return self._conn
|
||||||
try:
|
try:
|
||||||
# FYI: the connection process might raise a
|
# FYI: the connection process might raise a
|
||||||
# `ServerSelectionTimeoutError`, that is a subclass of
|
# `ServerSelectionTimeoutError`, that is a subclass of
|
||||||
@ -127,20 +129,20 @@ class LocalMongoDBConnection(DBConnection):
|
|||||||
if self.login is not None:
|
if self.login is not None:
|
||||||
client[self.dbname].authenticate(self.login,
|
client[self.dbname].authenticate(self.login,
|
||||||
mechanism='MONGODB-X509')
|
mechanism='MONGODB-X509')
|
||||||
|
self.__conn = client
|
||||||
return client
|
return client
|
||||||
|
|
||||||
except (pymongo.errors.ConnectionFailure,
|
except (pymongo.errors.ConnectionFailure,
|
||||||
pymongo.errors.OperationFailure) as exc:
|
pymongo.errors.OperationFailure) as exc:
|
||||||
logger.info('Exception in connect(): {}'.format(exc))
|
logger.info('Exception in connect(): {}'.format(exc))
|
||||||
raise ConnectionError(str(exc)) from exc
|
raise ConnectionError(str(exc)) from exc
|
||||||
except pymongo.errors.ConfigurationError as exc:
|
except pymongo.queryerrors.ConfigurationError as exc:
|
||||||
raise ConfigurationError from exc
|
raise ConfigurationError from exc
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
try:
|
try:
|
||||||
self.conn.close()
|
self.__conn.close()
|
||||||
self.conn = None
|
self.__conn = None
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.info('Exception in planetmint.backend.localmongodb.close(): {}'.format(exc))
|
logger.info('Exception in planetmint.backend.localmongodb.close(): {}'.format(exc))
|
||||||
raise ConnectionError(str(exc)) from exc
|
raise ConnectionError(str(exc)) from exc
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user