[bitbake-devel] [PATCH 15/18] toaster: work around 'database is locked' error

brian avery avery.brian at gmail.com
Thu Dec 10 03:56:40 UTC 2015


From: Ed Bartosh <ed.bartosh at linux.intel.com>

When sqlite can not cope with a stream of 'insert' queries it throws
'database is locked' exception.

Wrapping model.save in transaction.atomic context and repeating the call
should solve this issue.

Signed-off-by: Ed Bartosh <ed.bartosh at linux.intel.com>
Signed-off-by: brian avery <avery.brian at gmail.com>
---
 lib/toaster/orm/models.py | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/lib/toaster/orm/models.py b/lib/toaster/orm/models.py
index 3c51c6f..0d598ac 100644
--- a/lib/toaster/orm/models.py
+++ b/lib/toaster/orm/models.py
@@ -29,10 +29,28 @@ from django.core import validators
 from django.conf import settings
 import django.db.models.signals
 
-
 import logging
 logger = logging.getLogger("toaster")
 
+if 'sqlite' in settings.DATABASES['default']['ENGINE']:
+    from django.db import transaction, OperationalError
+    from time import sleep
+
+    _base_save = models.Model.save
+    def save(self, *args, **kwargs):
+        while True:
+            try:
+                with transaction.atomic():
+                    return _base_save(self, *args, **kwargs)
+            except OperationalError as err:
+                if 'database is locked' in str(err):
+                    logger.warning("%s, model: %s, args: %s, kwargs: %s",
+                                   err, self.__class__, args, kwargs)
+                    sleep(0.5)
+                    continue
+                raise
+
+    models.Model.save = save
 
 class GitURLValidator(validators.URLValidator):
     import re
-- 
1.9.1




More information about the bitbake-devel mailing list