Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit ab89cdb

Browse files
authored
Merge pull request #204 from biocommons/185-sqlite-database-connection-left-open-on-exit-during-testing
Closes #185: resolve sqlite database connections left open during tes…
2 parents b459d3d + fd4a2ae commit ab89cdb

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/biocommons/eutils/_internal/queryservice.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ def __init__(
144144
cache_path = False
145145
self._cache = SQLiteCache(cache_path) if cache_path else None
146146

147+
def close(self):
148+
"""Close the cache connection."""
149+
if self._cache is not None:
150+
self._cache.close()
151+
152+
def __del__(self):
153+
"""Ensure cache connection is closed on object destruction."""
154+
self.close()
155+
147156
def efetch(self, args):
148157
"""
149158
execute a cached, throttled efetch query

src/biocommons/eutils/_internal/sqlitecache.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""simple key-value cache with transparent payload compression and expiration
2-
3-
Taken from http://bitbucket.org/reece/rcore/
4-
"""
1+
"""simple key-value cache with transparent payload compression and expiration"""
52

63
import logging
74
import pickle
@@ -35,6 +32,26 @@ def __init__(self, db_path, compress_values=True):
3532
self._logger = logging.getLogger(__name__)
3633
self._connect(db_path)
3734

35+
def close(self):
36+
"""Close the database connection."""
37+
if self._con is not None:
38+
self._con.close()
39+
self._logger.info("closed %s", self._db_path)
40+
self._con = None
41+
42+
def __enter__(self):
43+
"""Context manager entry."""
44+
return self
45+
46+
def __exit__(self, exc_type, exc_val, exc_tb):
47+
"""Context manager exit."""
48+
self.close()
49+
return False
50+
51+
def __del__(self):
52+
"""Ensure connection is closed on object destruction."""
53+
self.close()
54+
3855
def expire(self, age):
3956
cur = self._execute("DELETE FROM cache WHERE strftime('%s','now') - created > ?", [age])
4057
return cur.rowcount

tests/test_rcore_sqllitecache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def setUp(self):
1515

1616
self.cache = SQLiteCache(self._fn)
1717

18+
def tearDown(self):
19+
self.cache.close()
20+
1821

1922
class Test_SQLiteCache_AttrLookup(Test_SQLiteCacheBase):
2023
def test_str_str(self):

0 commit comments

Comments
 (0)