From 537aed178316ea2f4e62ce5d7aa0a115c5d695d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Ng=E1=BB=8Dc=20=C4=90=E1=BB=A9c=20Huy?= Date: Wed, 21 Feb 2024 09:46:04 +0700 Subject: [PATCH] Close SQLite pool even when there is error Closes #58 --- src/mayim/sql/sqlite/interface.py | 35 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/mayim/sql/sqlite/interface.py b/src/mayim/sql/sqlite/interface.py index 9c283c3..fa9bc0a 100644 --- a/src/mayim/sql/sqlite/interface.py +++ b/src/mayim/sql/sqlite/interface.py @@ -55,20 +55,21 @@ async def connection(self, timeout: Optional[float] = None): existing = self.existing_connection() close_when_done = False - if existing: - yield existing - else: - if not self._db: - close_when_done = True - await self.open() - yield self._db - - transaction = self.in_transaction() - commit = self.do_commit() - - if not transaction: - if commit: - await self._db.commit() # type: ignore - - if close_when_done: - await self.close() + try: + if existing: + yield existing + else: + if not self._db: + close_when_done = True + await self.open() + yield self._db + + transaction = self.in_transaction() + commit = self.do_commit() + + if not transaction: + if commit: + await self._db.commit() # type: ignore + finally: + if close_when_done: + await self.close()