use with to close cursor

use settings for isolation in connect
dont re raise the exception in the __exit__
This commit is contained in:
shaun 2016-11-05 09:19:28 +11:00
parent 9783b5aec2
commit a763cd37c9
2 changed files with 24 additions and 26 deletions

View file

@ -7,7 +7,7 @@ import sqlite3
import xbmc import xbmc
from utils import window, should_stop from utils import window, should_stop, settings
################################################################################################# #################################################################################################
@ -79,22 +79,25 @@ def kodi_commit():
class DatabaseConn(object): class DatabaseConn(object):
# To be called as context manager - i.e. with DatabaseConn() as conn: #dostuff # To be called as context manager - i.e. with DatabaseConn() as conn: #dostuff
def __init__(self, database_file="video", commit_mode="", timeout=20): def __init__(self, database_file="video", commit_on_close=True, timeout=120):
""" """
database_file can be custom: emby, texture, music, video, :memory: or path to the file database_file can be custom: emby, texture, music, video, :memory: or path to the file
commit_mode set to None to autocommit (isolation_level). See python documentation. commit_mode set to None to autocommit (isolation_level). See python documentation.
""" """
self.db_file = database_file self.db_file = database_file
self.commit_mode = commit_mode self.commit_on_close = commit_on_close
self.timeout = timeout self.timeout = timeout
def __enter__(self): def __enter__(self):
# Open the connection # Open the connection
self.path = self._SQL(self.db_file) self.path = self._SQL(self.db_file)
log.info("opening database: %s", self.path) log.info("opening database: %s", self.path)
self.conn = sqlite3.connect(self.path,
isolation_level=self.commit_mode, if settings('dblock') == "true":
timeout=self.timeout) self.conn = sqlite3.connect(self.path, isolation_level=None, timeout=self.timeout)
else:
self.conn = sqlite3.connect(self.path, timeout=self.timeout)
return self.conn return self.conn
def _SQL(self, media_type): def _SQL(self, media_type):
@ -114,16 +117,10 @@ class DatabaseConn(object):
if exc_type is not None: if exc_type is not None:
# Errors were raised in the with statement # Errors were raised in the with statement
log.error("Type: %s Value: %s", exc_type, exc_val) log.error("Type: %s Value: %s", exc_type, exc_val)
if "database is locked" in exc_val:
self.conn.rollback()
else:
raise
elif self.commit_mode is not None and changes: if self.commit_on_close == True and changes:
log.info("number of rows updated: %s", changes) log.info("number of rows updated: %s", changes)
if self.db_file == "video" and kodi_commit(): kodi_commit()
self.conn.commit()
else:
self.conn.commit() self.conn.commit()
log.info("close: %s", self.path) log.info("close: %s", self.path)

View file

@ -25,6 +25,7 @@ import videonodes
from objects import Movies, MusicVideos, TVShows, Music from objects import Movies, MusicVideos, TVShows, Music
from utils import window, settings, language as lang, should_stop from utils import window, settings, language as lang, should_stop
from ga_client import GoogleAnalytics from ga_client import GoogleAnalytics
from contextlib import closing
################################################################################################## ##################################################################################################
@ -849,7 +850,7 @@ class LibrarySync(threading.Thread):
def _verify_emby_database(self): def _verify_emby_database(self):
# Create the tables for the emby database # Create the tables for the emby database
with self.database('emby') as conn: with self.database('emby') as conn:
cursor = conn.cursor() with closing(conn.cursor()) as cursor:
# emby, view, version # emby, view, version
cursor.execute( cursor.execute(
"""CREATE TABLE IF NOT EXISTS emby( """CREATE TABLE IF NOT EXISTS emby(