changeset 5615:d5707598fd64 stable

db: fix unknown exception type in commit error handling efce61aac33d was a blind fix. It failed because `from sqlalchemy import *` doesn't import exc and the new except clause would thus fail. It also failed because the session has to be rolled back after a commit failure. Now, rework it to fix these issues. Note that we are able to detect whether the commit failed for valid reasons ... but we can't use that information to much ...
author Mads Kiilerich <madski@unity3d.com>
date Tue, 05 Jan 2016 16:23:22 +0100
parents 2189802db18a
children 890189aa2bfe
files kallithea/model/db.py
diffstat 1 files changed, 10 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/model/db.py	Tue Jan 05 16:23:22 2016 +0100
+++ b/kallithea/model/db.py	Tue Jan 05 16:23:22 2016 +0100
@@ -34,6 +34,7 @@
 import collections
 import functools
 
+import sqlalchemy
 from sqlalchemy import *
 from sqlalchemy.ext.hybrid import hybrid_property
 from sqlalchemy.orm import relationship, joinedload, class_mapper, validates
@@ -2136,19 +2137,22 @@
             return True
 
         inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
-        if not inv_obj:
+        if inv_obj is None:
             inv_obj = CacheInvalidation(cache_key, repo_name)
-        if inv_obj.cache_active:
+        elif inv_obj.cache_active:
             return True
         inv_obj.cache_active = True
         Session().add(inv_obj)
         try:
             Session().commit()
-        except exc.IntegrityError:
+        except sqlalchemy.exc.IntegrityError:
+            log.error('commit of CacheInvalidation failed - retrying')
+            Session().rollback()
             inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
-            if not inv_obj:
-                raise
-            # TOCTOU - another thread added the key at the same time; no further action required
+            if inv_obj is None:
+                log.error('failed to create CacheInvalidation entry')
+                # TODO: fail badly?
+            # else: TOCTOU - another thread added the key at the same time; no further action required
         return False
 
     @classmethod