Skip to content

Commit e128744

Browse files
authored
Merge pull request #132 from mbhutton/fix/close-db-connection-on-weakref-finalize
Close DB connection when Database is garbage collected
2 parents a83d418 + dee457e commit e128744

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

things/database.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import plistlib
99
import re
1010
import sqlite3
11+
import weakref
1112
from textwrap import dedent
1213
from typing import Optional, Union
1314

@@ -186,6 +187,8 @@ 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+
# Close the underlying SQLite connection when this Database object is garbage collected
191+
weakref.finalize(self, sqlite3.Connection.close, self.connection)
189192

190193
# Test for migrated database in Things 3.15.16+
191194
# --------------------------------

0 commit comments

Comments
 (0)