Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/app/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@


def get_db() -> Generator[Session, None, None]:
with Session(engine) as session:
session = Session(engine)
try:
yield session
finally:
session.close()
Comment on lines +22 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Functional equivalence of context manager vs try/finally for Session

The old code used with Session(engine) as session: yield session, which relies on Session.__enter__ (returns self) and Session.__exit__ (calls self.close()). The new code manually creates the session, yields it, and calls session.close() in a finally block. These are functionally equivalent because SQLAlchemy's Session.__exit__ does nothing beyond calling close() — it does not auto-commit or auto-rollback based on exception status (unlike Session.begin() which does). However, the with statement pattern is more idiomatic and slightly more concise. The refactor doesn't appear to have a clear motivation — both patterns behave identically for this use case.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.



SessionDep = Annotated[Session, Depends(get_db)]
Expand Down