Amend DB Error OperationError

bzr-revno: 1988
This commit is contained in:
Dave Warnock 2012-06-11 18:33:57 +01:00 committed by Tim Bentley
commit e0c5fabc5a
1 changed files with 145 additions and 122 deletions

View File

@ -239,27 +239,30 @@ class Manager(object):
``commit`` ``commit``
Commit the session with this object Commit the session with this object
""" """
try: for try_count in range(3):
self.session.add(object_instance) try:
if commit: self.session.add(object_instance)
self.session.commit() if commit:
self.is_dirty = True self.session.commit()
return True self.is_dirty = True
except OperationalError: return True
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue - "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
self.session.add(object_instance) # way. So we only retry 3 times.
if commit: log.exception(u'Probably a MySQL issue - "MySQL has gone away"')
self.session.commit() self.session.rollback()
self.is_dirty = True if try_count >= 2:
return True raise
except InvalidRequestError: except InvalidRequestError:
self.session.rollback() self.session.rollback()
log.exception(u'Object save failed') log.exception(u'Object list save failed')
return False return False
except:
self.session.rollback()
raise
def save_objects(self, object_list, commit=True): def save_objects(self, object_list, commit=True):
""" """
@ -271,27 +274,30 @@ class Manager(object):
``commit`` ``commit``
Commit the session with this object Commit the session with this object
""" """
try: for try_count in range(3):
self.session.add_all(object_list) try:
if commit: self.session.add_all(object_list)
self.session.commit() if commit:
self.is_dirty = True self.session.commit()
return True self.is_dirty = True
except OperationalError: return True
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
self.session.add_all(object_list) # way. So we only retry 3 times.
if commit: log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
self.session.commit() self.session.rollback()
self.is_dirty = True if try_count >= 2:
return True raise
except InvalidRequestError: except InvalidRequestError:
self.session.rollback() self.session.rollback()
log.exception(u'Object list save failed') log.exception(u'Object list save failed')
return False return False
except:
self.session.rollback()
raise
def get_object(self, object_class, key=None): def get_object(self, object_class, key=None):
""" """
@ -306,15 +312,18 @@ class Manager(object):
if not key: if not key:
return object_class() return object_class()
else: else:
try: for try_count in range(3):
return self.session.query(object_class).get(key) try:
except OperationalError: return self.session.query(object_class).get(key)
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
return self.session.query(object_class).get(key) # way. So we only retry 3 times.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
if try_count >= 2:
raise
def get_object_filtered(self, object_class, filter_clause): def get_object_filtered(self, object_class, filter_clause):
""" """
@ -326,15 +335,18 @@ class Manager(object):
``filter_clause`` ``filter_clause``
The criteria to select the object by The criteria to select the object by
""" """
try: for try_count in range(3):
return self.session.query(object_class).filter(filter_clause).first() try:
except OperationalError: return self.session.query(object_class).filter(filter_clause).first()
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
return self.session.query(object_class).filter(filter_clause).first() # way. So we only retry 3 times.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
if try_count >= 2:
raise
def get_all_objects(self, object_class, filter_clause=None, def get_all_objects(self, object_class, filter_clause=None,
order_by_ref=None): order_by_ref=None):
@ -358,15 +370,18 @@ class Manager(object):
query = query.order_by(*order_by_ref) query = query.order_by(*order_by_ref)
elif order_by_ref is not None: elif order_by_ref is not None:
query = query.order_by(order_by_ref) query = query.order_by(order_by_ref)
try: for try_count in range(3):
return query.all() try:
except OperationalError: return query.all()
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
return query.all() # way. So we only retry 3 times.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
if try_count >= 2:
raise
def get_object_count(self, object_class, filter_clause=None): def get_object_count(self, object_class, filter_clause=None):
""" """
@ -382,15 +397,18 @@ class Manager(object):
query = self.session.query(object_class) query = self.session.query(object_class)
if filter_clause is not None: if filter_clause is not None:
query = query.filter(filter_clause) query = query.filter(filter_clause)
try: for try_count in range(3):
return query.count() try:
except OperationalError: return query.count()
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
return query.count() # way. So we only retry 3 times.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
if try_count >= 2:
raise
def delete_object(self, object_class, key): def delete_object(self, object_class, key):
""" """
@ -404,25 +422,29 @@ class Manager(object):
""" """
if key != 0: if key != 0:
object_instance = self.get_object(object_class, key) object_instance = self.get_object(object_class, key)
try: for try_count in range(3):
self.session.delete(object_instance) try:
self.session.commit() self.session.delete(object_instance)
self.is_dirty = True self.session.commit()
return True self.is_dirty = True
except OperationalError: return True
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
self.session.delete(object_instance) # way. So we only retry 3 times.
self.session.commit() log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
self.is_dirty = True self.session.rollback()
return True if try_count >= 2:
except InvalidRequestError: raise
self.session.rollback() except InvalidRequestError:
log.exception(u'Failed to delete object') self.session.rollback()
return False log.exception(u'Failed to delete object')
return False
except:
self.session.rollback()
raise
else: else:
return True return True
@ -440,31 +462,32 @@ class Manager(object):
The filter governing selection of objects to return. Defaults to The filter governing selection of objects to return. Defaults to
None. None.
""" """
try: for try_count in range(3):
query = self.session.query(object_class) try:
if filter_clause is not None: query = self.session.query(object_class)
query = query.filter(filter_clause) if filter_clause is not None:
query.delete(synchronize_session=False) query = query.filter(filter_clause)
self.session.commit() query.delete(synchronize_session=False)
self.is_dirty = True self.session.commit()
return True self.is_dirty = True
except OperationalError: return True
# This exception clause is for users running MySQL which likes except OperationalError:
# to terminate connections on its own without telling anyone. # This exception clause is for users running MySQL which likes
# See bug #927473 # to terminate connections on its own without telling anyone.
log.exception(u'Probably a MySQL issue, "MySQL has gone away"') # See bug #927473
self.session.rollback() # However, other dbms can raise it, usually in a non-recoverable
query = self.session.query(object_class) # way. So we only retry 3 times.
if filter_clause is not None: log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
query = query.filter(filter_clause) self.session.rollback()
query.delete(synchronize_session=False) if try_count >= 2:
self.session.commit() raise
self.is_dirty = True except InvalidRequestError:
return True self.session.rollback()
except InvalidRequestError: log.exception(u'Failed to delete %s records', object_class.__name__)
self.session.rollback() return False
log.exception(u'Failed to delete %s records', object_class.__name__) except:
return False self.session.rollback()
raise
def finalise(self): def finalise(self):
""" """