← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~raoul-snyman/openlp/bug-927473 into lp:openlp

 

Raoul Snyman has proposed merging lp:~raoul-snyman/openlp/bug-927473 into lp:openlp.

Requested reviews:
  Jon Tibble (meths)

For more details, see:
https://code.launchpad.net/~raoul-snyman/openlp/bug-927473/+merge/93493

Attempted to fix bug #927473 by catching operational errors and redoing the action.
-- 
https://code.launchpad.net/~raoul-snyman/openlp/bug-927473/+merge/93493
Your team OpenLP Core is subscribed to branch lp:openlp.
=== modified file 'openlp/core/lib/db.py'
--- openlp/core/lib/db.py	2011-12-27 10:33:55 +0000
+++ openlp/core/lib/db.py	2012-02-16 21:21:28 +0000
@@ -239,6 +239,17 @@
                 self.session.commit()
             self.is_dirty = True
             return True
+        except OperationalError:
+            # This exception clause is for users running MySQL which likes
+            # to terminate connections on its own without telling anyone.
+            # See bug #927473
+            log.exception(u'Probably a MySQL issue - "MySQL has gone away"')
+            self.session.rollback()
+            self.session.add(object_instance)
+            if commit:
+                self.session.commit()
+            self.is_dirty = True
+            return True
         except InvalidRequestError:
             self.session.rollback()
             log.exception(u'Object save failed')
@@ -260,6 +271,17 @@
                 self.session.commit()
             self.is_dirty = True
             return True
+        except OperationalError:
+            # This exception clause is for users running MySQL which likes
+            # to terminate connections on its own without telling anyone.
+            # See bug #927473
+            log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+            self.session.rollback()
+            self.session.add_all(object_list)
+            if commit:
+                self.session.commit()
+            self.is_dirty = True
+            return True
         except InvalidRequestError:
             self.session.rollback()
             log.exception(u'Object list save failed')
@@ -278,7 +300,15 @@
         if not key:
             return object_class()
         else:
-            return self.session.query(object_class).get(key)
+            try:
+                return self.session.query(object_class).get(key)
+            except OperationalError:
+                # This exception clause is for users running MySQL which likes
+                # to terminate connections on its own without telling anyone.
+                # See bug #927473
+                log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+                self.session.rollback()
+                return self.session.query(object_class).get(key)
 
     def get_object_filtered(self, object_class, filter_clause):
         """
@@ -290,7 +320,15 @@
         ``filter_clause``
             The criteria to select the object by
         """
-        return self.session.query(object_class).filter(filter_clause).first()
+        try:
+            return self.session.query(object_class).filter(filter_clause).first()
+        except OperationalError:
+            # This exception clause is for users running MySQL which likes
+            # to terminate connections on its own without telling anyone.
+            # See bug #927473
+            log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+            self.session.rollback()
+            return self.session.query(object_class).filter(filter_clause).first()
 
     def get_all_objects(self, object_class, filter_clause=None,
         order_by_ref=None):
@@ -311,10 +349,18 @@
         if filter_clause is not None:
             query = query.filter(filter_clause)
         if isinstance(order_by_ref, list):
-            return query.order_by(*order_by_ref).all()
+            query = query.order_by(*order_by_ref)
         elif order_by_ref is not None:
-            return query.order_by(order_by_ref).all()
-        return query.all()
+            query = query.order_by(order_by_ref)
+        try:
+            return query.all()
+        except OperationalError:
+            # This exception clause is for users running MySQL which likes
+            # to terminate connections on its own without telling anyone.
+            # See bug #927473
+            log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+            self.session.rollback()
+            return query.all()
 
     def get_object_count(self, object_class, filter_clause=None):
         """
@@ -330,7 +376,15 @@
         query = self.session.query(object_class)
         if filter_clause is not None:
             query = query.filter(filter_clause)
-        return query.count()
+        try:
+            return query.count()
+        except OperationalError:
+            # This exception clause is for users running MySQL which likes
+            # to terminate connections on its own without telling anyone.
+            # See bug #927473
+            log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+            self.session.rollback()
+            return query.count()
 
     def delete_object(self, object_class, key):
         """
@@ -349,6 +403,16 @@
                 self.session.commit()
                 self.is_dirty = True
                 return True
+            except OperationalError:
+                # This exception clause is for users running MySQL which likes
+                # to terminate connections on its own without telling anyone.
+                # See bug #927473
+                log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+                self.session.rollback()
+                self.session.delete(object_instance)
+                self.session.commit()
+                self.is_dirty = True
+                return True
             except InvalidRequestError:
                 self.session.rollback()
                 log.exception(u'Failed to delete object')
@@ -378,6 +442,19 @@
             self.session.commit()
             self.is_dirty = True
             return True
+        except OperationalError:
+            # This exception clause is for users running MySQL which likes
+            # to terminate connections on its own without telling anyone.
+            # See bug #927473
+            log.exception(u'Probably a MySQL issue, "MySQL has gone away"')
+            self.session.rollback()
+            query = self.session.query(object_class)
+            if filter_clause is not None:
+                query = query.filter(filter_clause)
+            query.delete(synchronize_session=False)
+            self.session.commit()
+            self.is_dirty = True
+            return True
         except InvalidRequestError:
             self.session.rollback()
             log.exception(u'Failed to delete %s records', object_class.__name__)


Follow ups