From 1594f58f92c23658a199a690e55d8ae03182affd Mon Sep 17 00:00:00 2001 From: Matt Hutton Date: Sun, 23 Feb 2025 10:41:15 +1300 Subject: [PATCH] Revert "fix: Improve query performance by using a single connection" This reverts commit 0ed2f9db1de284fcff13e6afba2fd3fa2e086477. This addresses a CI test warning: 'ResourceWarning: unclosed database'. --- things/database.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/things/database.py b/things/database.py index 5bbd7da..ab0a050 100755 --- a/things/database.py +++ b/things/database.py @@ -168,7 +168,6 @@ class Database: """ debug = False - connection: sqlite3.Connection # pylint: disable=R0913 def __init__(self, filepath=None, print_sql=False): @@ -182,11 +181,6 @@ def __init__(self, filepath=None, print_sql=False): if self.print_sql: self.execute_query_count = 0 - # "ro" means read-only - # See: https://sqlite.org/uri.html#recognized_query_parameters - uri = f"file:{self.filepath}?mode=ro" - self.connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101 - # Test for migrated database in Things 3.15.16+ # -------------------------------- assert self.get_version() > 21, ( @@ -501,14 +495,17 @@ def execute_query(self, sql_query, parameters=(), row_factory=None): print(prettify_sql(sql_query)) print() - with self.connection: - # Using context manager to keep queries in separate transactions, - # see https://docs.python.org/3/library/sqlite3.html#sqlite3-connection-context-manager - self.connection.row_factory = row_factory or dict_factory - cursor = self.connection.cursor() - cursor.execute(sql_query, parameters) + # "ro" means read-only + # See: https://sqlite.org/uri.html#recognized_query_parameters + uri = f"file:{self.filepath}?mode=ro" + connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101 + connection.row_factory = row_factory or dict_factory + cursor = connection.cursor() + cursor.execute(sql_query, parameters) - return cursor.fetchall() + result = cursor.fetchall() + connection.close() + return result # Helper functions