Commit 86f64828 authored by Julien Muchembled's avatar Julien Muchembled

storage: retry instead of raise if the DB is locked by another process

This changes is similar to commit 3d0fab20
("logger: retry instead of raise if the log is locked by another process").
parent 04922832
......@@ -19,6 +19,7 @@ from array import array
from hashlib import sha1
import re
import string
import traceback
from . import DatabaseManager, LOG_QUERIES
from .manager import CreationUndone
......@@ -36,6 +37,23 @@ def splitOIDField(tid, oids):
append(oids[i:i+8])
return oid_list
def retry_if_locked(f, *args):
try:
return f(*args)
except sqlite3.OperationalError, e:
x = e.args[0]
if x == 'database is locked':
msg = traceback.format_exception_only(type(e), e)
msg += traceback.format_stack()
logging.warning(''.join(msg))
while e.args[0] == x:
try:
return f(*args)
except sqlite3.OperationalError, e:
pass
raise
class SQLiteDatabaseManager(DatabaseManager):
"""This class manages a database on SQLite.
......@@ -63,13 +81,13 @@ class SQLiteDatabaseManager(DatabaseManager):
def begin(self):
q = self.query
q("BEGIN IMMEDIATE")
retry_if_locked(q, "BEGIN IMMEDIATE")
return q
if LOG_QUERIES:
def commit(self):
logging.debug('committing...')
self.conn.commit()
retry_if_locked(self.conn.commit)
def rollback(self):
logging.debug('aborting...')
......@@ -84,9 +102,10 @@ class SQLiteDatabaseManager(DatabaseManager):
logging.debug('querying %s...', ''.join(printable_char_list))
return self.conn.execute(query)
else:
commit = property(lambda self: self.conn.commit)
rollback = property(lambda self: self.conn.rollback)
query = property(lambda self: self.conn.execute)
def commit(self):
retry_if_locked(self.conn.commit)
def setup(self, reset = 0):
self._config.clear()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment