Wrap Kodi DB transactions explicitly

This commit is contained in:
croneter 2018-11-10 16:09:06 +01:00
parent 48f5d67d63
commit be45d914d3
3 changed files with 25 additions and 12 deletions

View file

@ -56,10 +56,11 @@ class ItemBase(object):
""" """
self.plexconn = utils.kodi_sql('plex') self.plexconn = utils.kodi_sql('plex')
self.plexcursor = self.plexconn.cursor() self.plexcursor = self.plexconn.cursor()
self.kodiconn = utils.kodi_sql('video')
self.kodicursor = self.kodiconn.cursor()
self.artconn = utils.kodi_sql('texture') self.artconn = utils.kodi_sql('texture')
self.artcursor = self.artconn.cursor() self.artcursor = self.artconn.cursor()
self.kodiconn = utils.kodi_sql('video', writer=True)
self.kodicursor = self.kodiconn.cursor()
self.kodicursor.execute('BEGIN')
self.plexdb = PlexDB(self.plexcursor) self.plexdb = PlexDB(self.plexcursor)
self.kodidb = KodiVideoDB(texture_db=True, self.kodidb = KodiVideoDB(texture_db=True,
cursor=self.kodicursor, cursor=self.kodicursor,
@ -71,13 +72,20 @@ class ItemBase(object):
Make sure DB changes are committed and connection to DB is closed. Make sure DB changes are committed and connection to DB is closed.
""" """
self.plexconn.commit() self.plexconn.commit()
self.kodiconn.commit()
self.artconn.commit()
self.plexconn.close() self.plexconn.close()
self.kodiconn.close() self.artconn.commit()
self.artconn.close() self.artconn.close()
self.kodicursor.execute('END TRANSACTION')
self.kodiconn.close()
return self return self
def commit(self):
self.plexconn.commit()
self.artconn.commit()
self.kodicursor.execute('END TRANSACTION')
self.kodicursor.execute('PRAGMA wal_checkpoint(FULL);')
self.kodicursor.execute('BEGIN TRANSACTION')
def set_fanart(self, artworks, kodi_id, kodi_type): def set_fanart(self, artworks, kodi_id, kodi_type):
""" """
Writes artworks [dict containing only set artworks] to the Kodi art DB Writes artworks [dict containing only set artworks] to the Kodi art DB

View file

@ -123,12 +123,12 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
self.processed += 1 self.processed += 1
self.update_progressbar() self.update_progressbar()
self.current += 1 self.current += 1
if self.processed == 500: if self.processed == 200:
self.processed = 0 self.processed = 0
context.kodiconn.commit() context.commit()
context.artconn.commit()
context.plexconn.commit()
self.queue.task_done() self.queue.task_done()
if section.plex_type == 'episode' and self.current == 1000:
break
profile.disable() profile.disable()
string_io = StringIO() string_io = StringIO()
stats = Stats(profile, stream=string_io).sort_stats('cumulative') stats = Stats(profile, stream=string_io).sort_stats('cumulative')

View file

@ -475,7 +475,7 @@ def unix_timestamp(seconds_into_the_future=None):
return int((future - EPOCH).total_seconds()) return int((future - EPOCH).total_seconds())
def kodi_sql(media_type=None): def kodi_sql(media_type=None, writer=False):
""" """
Open a connection to the Kodi database. Open a connection to the Kodi database.
media_type: 'video' (standard if not passed), 'plex', 'music', 'texture' media_type: 'video' (standard if not passed), 'plex', 'music', 'texture'
@ -488,8 +488,13 @@ def kodi_sql(media_type=None):
db_path = v.DB_TEXTURE_PATH db_path = v.DB_TEXTURE_PATH
else: else:
db_path = v.DB_VIDEO_PATH db_path = v.DB_VIDEO_PATH
conn = connect(db_path, timeout=5.0) if writer:
conn.execute('PRAGMA journal_mode=WAL') conn = connect(db_path, timeout=5.0, isolation_level=None)
conn.execute('PRAGMA journal_mode=WAL')
# conn.execute('BEGIN TRANSACTION')
else:
conn = connect(db_path, timeout=5.0)
conn.execute('PRAGMA journal_mode=WAL')
return conn return conn