Commit e86b5551 authored by Antonin Dvorak's avatar Antonin Dvorak
Browse files

final fix for sqlite3 wrapper

parent 9775b809
...@@ -5,9 +5,7 @@ def modify_db(conn, ...@@ -5,9 +5,7 @@ def modify_db(conn,
query, query,
*args): *args):
"""Wrapper for modifications (INSERT, UPDATE, DELETE or REPLACE) of Sqlite3 database. """Wrapper for modifications (INSERT, UPDATE, DELETE or REPLACE) of Sqlite3 database.
Executes given query and commits it - in case of lock it retries the commit Executes given query and commits it - in case of lock it retries the commit
Params Params
------ ------
connection: sqlite3 connection connection: sqlite3 connection
...@@ -17,31 +15,39 @@ def modify_db(conn, ...@@ -17,31 +15,39 @@ def modify_db(conn,
args: array args: array
additional arguments for execute query, optional additional arguments for execute query, optional
""" """
upperbound = 11
with conn: with conn:
c = conn.cursor() c = conn.cursor()
for x in range(1, 11): # retry db execute
for x in range(1, upperbound):
try: try:
if args: if args:
c.execute(query, args) c.execute(query, *args)
else: else:
c.execute(query) c.execute(query)
except: except Exception as e:
print("Sqlite3 execute unsuccessful, retrying after %s sec...." %x) print("Sqlite3 execute unsuccessful, reason: \"%s\" \nRetrying after %s sec...." % (str(e), x))
time.sleep(x) time.sleep(x)
pass pass
else: else:
print("Sqlite3 execute successful, breaking the retry cycle.") print("Sqlite3 execute successful, breaking the retry cycle.")
break break
# max attempts achieved, quit
if x == upperbound - 1:
raise Exception("Sqlite3 - achieved max retries to execute with no success, giving up...")
# retry commit - db might be locked by different process # retry db commit
for x in range(1, 11): for x in range(1, upperbound):
try: try:
conn.commit() conn.commit()
except: except Exception as e:
print("Sqlite3 commit unsuccessful, retrying after %s sec...." %x) print("Sqlite3 commit unsuccessful, reason: \"%s\" \nRetrying after %s sec...." % (str(e), x))
time.sleep(x) time.sleep(x)
pass pass
else: else:
print("Sqlite3 commit successful, breaking the retry cycle.") print("Sqlite3 commit successful, breaking the retry cycle.")
break break
# max attempts achieved, quit
if x == upperbound - 1:
raise Exception("Sqlite3 - achieved max retries to commit with no success, giving up...")
Supports Markdown
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