use common db query execution code that handles database locks

This commit is contained in:
Jude Nelson
2018-01-29 19:08:55 -05:00
parent 6a135a035f
commit 4afe4ab356

View File

@@ -41,6 +41,7 @@ from ..operations import *
from ..hashing import *
from ..scripts import *
from ..b40 import *
from ..util import db_query_execute
import virtualchain
@@ -471,7 +472,6 @@ def namedb_delete_prepare( cur, primary_key, primary_key_value, table_name ):
DO NOT CALL THIS METHOD DIRETLY
"""
# primary key corresponds to a real column
namedb_assert_fields_match( cur, {primary_key: primary_key_value}, table_name, columns_match_record=False )
@@ -485,39 +485,16 @@ def namedb_format_query( query, values ):
Turn a query into a string for printing.
Useful for debugging.
"""
return "".join( ["%s %s" % (frag, "'%s'" % val if type(val) in [str, unicode] else val) for (frag, val) in zip(query.split("?"), values + ("",))] )
def namedb_query_execute( cur, query, values ):
"""
Execute a query. If it fails, exit.
Execute a query. If it fails, abort. Retry with timeouts on lock
DO NOT CALL THIS DIRECTLY.
"""
timeout = 1.0
while True:
try:
ret = cur.execute( query, values )
return ret
except sqlite3.OperationalError as oe:
if oe.message == "database is locked":
timeout = timeout * 2 + timeout * random.random()
log.error("Query timed out due to lock; retrying in %s: %s" % (timeout, namedb_format_query( query, values )))
time.sleep(timeout)
else:
log.exception(oe)
log.error("FATAL: failed to execute query (%s, %s)" % (query, values))
log.error("\n".join(traceback.format_stack()))
os.abort()
except Exception, e:
log.exception(e)
log.error("FATAL: failed to execute query (%s, %s)" % (query, values))
log.error("\n".join(traceback.format_stack()))
os.abort()
return db_query_execute(cur, query, values)
def namedb_preorder_insert( cur, preorder_rec ):