Skip to content

Commit 26bbbb4

Browse files
committed
DRAFT: close DB connection in a weakref.finalize step
1 parent cca936f commit 26bbbb4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

things/database.py

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sqlite3
1111
from textwrap import dedent
1212
from typing import Optional, Union
13+
import weakref
1314

1415

1516
# --------------------------------------------------
@@ -186,6 +187,7 @@ def __init__(self, filepath=None, print_sql=False):
186187
# See: https://sqlite.org/uri.html#recognized_query_parameters
187188
uri = f"file:{self.filepath}?mode=ro"
188189
self.connection = sqlite3.connect(uri, uri=True) # pylint: disable=E1101
190+
self._finalizer = weakref.finalize(self, self._close_connection)
189191

190192
# Test for migrated database in Things 3.15.16+
191193
# --------------------------------
@@ -510,6 +512,13 @@ def execute_query(self, sql_query, parameters=(), row_factory=None):
510512

511513
return cursor.fetchall()
512514

515+
def _close_connection(self):
516+
"""Close the database connection."""
517+
try:
518+
self.connection.close()
519+
except:
520+
pass
521+
513522

514523
# Helper functions
515524

0 commit comments

Comments
 (0)