Skip to content

Commit 2e65f45

Browse files
committed
fix(db): improve exception handling in database context manager
1 parent fbd29f2 commit 2e65f45

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

app/db/base.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,21 @@ async def __aenter__(self):
4141
return self.db
4242

4343
async def __aexit__(self, exc_type, exc_value, traceback):
44-
if isinstance(exc_value, SQLAlchemyError):
45-
await self.db.rollback() # rollback on exception
46-
47-
await self.db.close()
44+
try:
45+
if exc_type is not None:
46+
# Rollback on any exception
47+
await self.db.rollback()
48+
except Exception:
49+
try:
50+
await self.db.rollback()
51+
except Exception:
52+
pass
53+
finally:
54+
# Always close the session to return connection to pool
55+
try:
56+
await self.db.close()
57+
except Exception:
58+
pass
4859

4960

5061
async def get_db(): # Dependency

node

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b243ae200aef45239ea78709251ab0d7a1eab9c1

node_bridge_py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 5e26e511abcda6d8ec5fc4c388f2281a48a09db6

0 commit comments

Comments
 (0)