diff sat/memory/sqlite.py @ 2746:1e2f0856c845

memory (sqlite): retry _runInteraction in the same way as for _runQuery, this improve reliability
author Goffi <goffi@goffi.org>
date Fri, 04 Jan 2019 09:21:16 +0100
parents ba74914277cf
children 6487b8855c9a
line wrap: on
line diff
--- a/sat/memory/sqlite.py	Fri Jan 04 09:20:32 2019 +0100
+++ b/sat/memory/sqlite.py	Fri Jan 04 09:21:16 2019 +0100
@@ -140,16 +140,37 @@
             #        Sqlite integration, probably with high level library
             retry -= 1
             if retry == 0:
-                log.error(_(u'too many db tries, we abandon! Error message: {msg}').format(
-                msg = e))
+                log.error(_(u'too many db tries, we abandon! Error message: {msg}')
+                    .format(msg = e))
                 raise e
-            log.warning(_(u'exception while running query, retrying ({try_}): {msg}').format(
+            log.warning(
+                _(u'exception while running query, retrying ({try_}): {msg}').format(
                 try_ = 6 - retry,
                 msg = e))
             kw['query_retry'] = retry
             return self._runQuery(trans, *args, **kw)
         return trans.fetchall()
 
+    def _runInteraction(self, interaction, *args, **kw):
+        # sometimes interaction may fail while committing in _runInteraction
+        # and it may be due to a db lock. So we work around it in a similar way
+        # as for _runQuery but with only 3 tries
+        retry = kw.pop('interaction_retry', 4)
+        try:
+            return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
+        except Exception as e:
+            retry -= 1
+            if retry == 0:
+                log.error(
+                    _(u'too many interaction tries, we abandon! Error message: {msg}')
+                    .format(msg = e))
+                raise e
+            log.warning(
+                _(u'exception while running interaction, retrying ({try_}): {msg}')
+                .format(try_ = 4 - retry, msg = e))
+            kw['interaction_retry'] = retry
+            return self._runInteraction(interaction, *args, **kw)
+
 
 class SqliteStorage(object):
     """This class manage storage with Sqlite database"""