-
Notifications
You must be signed in to change notification settings - Fork 27
FEAT: Context manager support for connection and cursor class #160
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
Conversation
|
Why, out of interest @jahnvi480 , should the context managers not close the connection/cursor? I am aware that you state that this aligns with |
… jahnvi/context_manager
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.
Pull Request Overview
This pull request adds context manager support (__enter__ and __exit__ methods) to both the Connection and Cursor classes, enabling their use with Python's with statement. The implementation follows pyodbc behavior where context managers commit transactions on normal exit and rollback on exceptions, but do not close the connection/cursor itself.
Key changes:
- Context manager methods added to both Connection and Cursor classes with transaction management
- Enhanced logging in Connection.close method to track rollback operations
- Comprehensive test coverage for various context manager scenarios including autocommit modes, exceptions, and nested usage
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| mssql_python/connection.py | Adds __enter__ and __exit__ methods with transaction commit/rollback logic and enhanced logging |
| mssql_python/cursor.py | Adds __enter__ and __exit__ methods that delegate transaction management to the connection |
| tests/test_003_connection.py | Comprehensive context manager tests for connections including edge cases and contextlib.closing usage |
| tests/test_004_cursor.py | Extensive cursor context manager tests covering various scenarios and error conditions |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…/mssql-python into jahnvi/context_manager
…/mssql-python into jahnvi/context_manager
sumitmsft
left a comment
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.
Good PR, we have had several discussions on this over a period of time and this has come out nicely.
In totality, following are the scenarios captured and their corresponding outcome:
Feature/Scenario and Outcomes:
Connection as Context Manager
Begins transaction on enter. On exit: commit if no error, rollback if error, always closes connection.
Cursor as Context Manager
Opens cursor on enter. On exit: closes cursor and releases resources. Does not close connection/transaction.
Automatic Commit/Rollback
Commits or rolls back transaction on context exit depending on success/error.
Resource Cleanup/No Leaks
All cursors and connections closed on exit or error, even if explicit close not called.
Nested Usage (multiple cursors from same connection)
Multiple cursors can be opened in nested/parallel contexts. Connection is reused until explicitly closed.
Destructor Safety Net
Cursor/connection resources are cleaned up even if not closed explicitly.
No Transaction/Connection Closure on Cursor Close
Closing a cursor only affects that cursor; connection/transaction remain open/active.
All Cursors Closed on Connection Close
When connection is closed, all open cursors are closed automatically.
Idempotency and Safety on Multiple Close Calls
Closing connection or cursor multiple times is safe and does not cause errors.
Compliance with Python DB-API 2.0 (PEP 249) context manager pattern
Both Connection and Cursor implement enter/exit as per DB-API best practices.
Work Item / Issue Reference
Summary
This pull request introduces context manager support for the
ConnectionandCursorclasses in themssql_pythonmodule, aligning their behavior withpyodbc. It also adds extensive tests to ensure correct functionality, covering various scenarios such as normal exits, exceptions, nested transactions, and manual commit/rollback. The key changes are grouped below:Context Manager Support
__enter__and__exit__methods to theConnectionclass to enable usage with thewithstatement. The connection commits transactions on normal exit and rolls back on exceptions, without closing the connection.__enter__and__exit__methods to theCursorclass to allow usage with thewithstatement. The cursor commits transactions on normal exit but does not close itself, aligning withpyodbcbehavior.Logging Enhancements
Connection.closemethod to log when uncommitted changes are rolled back before closing.Test Coverage
tests/test_003_connection.pyto verify context manager behavior for connections, including:contextlib.closingwith connections to ensure proper closure after context exit.tests/test_004_cursor.pyto includecontextlib.closingfor cursor tests.