-
Notifications
You must be signed in to change notification settings - Fork 51
FIX: Context manager commits on clean exit, rolls back on exception #639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b3c915a
00e2322
c0830ac
d43ebdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1703,16 +1703,43 @@ def __enter__(self) -> "Connection": | |
| logger.info("Entering connection context manager.") | ||
| return self | ||
|
|
||
| def __exit__(self, *args: Any) -> None: | ||
| def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: | ||
| """ | ||
| Exit the context manager. | ||
|
|
||
| Closes the connection when exiting the context, ensuring proper | ||
| resource cleanup. This follows the modern standard used by most | ||
| database libraries. | ||
| Implements commit-on-success / rollback-on-exception semantics: | ||
| - If the block exits cleanly and autocommit is off, the transaction | ||
| is committed. | ||
| - If an exception is raised and autocommit is off, the transaction | ||
| is rolled back. | ||
| - The connection is always closed when leaving the block. | ||
|
|
||
| If commit() fails on clean exit, the connection is closed and the | ||
| commit exception is raised. On exception exit, cleanup failures | ||
| (rollback or close) are suppressed so the original user exception | ||
| propagates unchanged. | ||
| """ | ||
| if not self._closed: | ||
| if self._closed: | ||
| return | ||
| try: | ||
| if not self.autocommit: | ||
| if exc_type is None: | ||
| self.commit() | ||
| else: | ||
| self.rollback() | ||
| except Exception: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| try: | ||
| self.close() | ||
| except Exception: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here as well... Would logging make sense? |
||
| pass | ||
| if exc_type is None: | ||
| raise | ||
| return | ||
| try: | ||
| self.close() | ||
| except Exception: | ||
| if exc_type is None: | ||
| raise | ||
|
|
||
| def __del__(self) -> None: | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
*args→exc_type, exc_val, exc_tb:the old
__exit__didn't inspect its arguments at all (just calledclose()), so*argswas fine. now we branch onexc_typeto decide commit vs rollback, so we use the canonical__exit__(self, exc_type, exc_val, exc_tb)signature from the Python data model.this is what
sqlite3,psycopg2, and every other DB-API driver uses. no behavioral change from the rename itself - just makes the branching logic readable.