1
0
mirror of https://github.com/moparisthebest/SickRage synced 2025-01-07 03:48:02 -05:00

Possible fix for database threading issues related to async calls from webui

This commit is contained in:
echel0n 2014-12-08 05:46:30 -08:00
parent d7164308a5
commit d2b6145f8c

View File

@ -30,9 +30,6 @@ from sickbeard import encodingKludge as ek
from sickbeard import logger
from sickbeard.exceptions import ex
db_lock = threading.Lock()
def dbFilename(filename="sickbeard.db", suffix=None):
"""
@param filename: The sqlite database filename to use. If not specified,
@ -46,13 +43,14 @@ def dbFilename(filename="sickbeard.db", suffix=None):
return ek.ek(os.path.join, sickbeard.DATA_DIR, filename)
class DBConnection(object):
class DBConnection(threading.Thread):
def __init__(self, filename="sickbeard.db", suffix=None, row_type=None):
self.filename = filename
self.suffix = suffix
self.row_type = row_type
self.connection = None
self.db_lock = threading.Lock()
try:
self.reconnect()
@ -64,17 +62,19 @@ class DBConnection(object):
"""Closes the existing database connection and re-opens it."""
self.close()
self.connection = sqlite3.connect(dbFilename(self.filename, self.suffix), 20, check_same_thread=False)
self.connection.isolation_level = None
self.connection.execute("pragma synchronous = off")
self.connection.execute("pragma temp_store = memory")
self.connection.execute("pragma journal_mode = memory")
self.connection.execute("pragma secure_delete = false")
self.connection.execute("pragma foreign_keys = on")
self.connection.text_factory = self._unicode_text_factory
self.connection.isolation_level = None
if self.row_type == "dict":
self.connection.row_factory = self._dict_factory
else:
self.connection.row_factory = sqlite3.Row
def __del__(self):
self.close()
def _cursor(self):
"""Returns the cursor; reconnects if disconnected."""
if self.connection is None: self.reconnect()
@ -82,8 +82,8 @@ class DBConnection(object):
def execute(self, query, args=None, fetchall=False, fetchone=False):
"""Executes the given query, returning the lastrowid from the query."""
cursor = self._cursor()
cursor = self._cursor()
try:
if fetchall:
return self._execute(cursor, query, args).fetchall()
@ -102,7 +102,9 @@ class DBConnection(object):
except:
pass
return x
try:
with self.db_lock:
if not args:
return cursor.execute(query)
#args = map(convert, args)
@ -128,8 +130,6 @@ class DBConnection(object):
return 0
def mass_action(self, querylist=[], logTransaction=False, fetchall=False):
with db_lock:
# remove None types
querylist = [i for i in querylist if i is not None]
@ -175,9 +175,6 @@ class DBConnection(object):
return sqlResult
def action(self, query, args=None, fetchall=False, fetchone=False):
with db_lock:
if query == None:
return
@ -246,8 +243,6 @@ class DBConnection(object):
self.action(query, valueDict.values() + keyDict.values())
def tableInfo(self, tableName):
# FIXME ? binding is not supported here, but I cannot find a way to escape a string manually
sqlResult = self.select("PRAGMA table_info(%s)" % tableName)
columns = {}
for column in sqlResult:
@ -257,7 +252,6 @@ class DBConnection(object):
def _unicode_text_factory(self, x):
return unicode(x, 'utf-8')
# http://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite-query
def _dict_factory(self, cursor, row):
d = {}
for idx, col in enumerate(cursor.description):