-
Notifications
You must be signed in to change notification settings - Fork 16
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
base: main
Are you sure you want to change the base?
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 <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
…/mssql-python into jahnvi/context_manager
I have to strongly agree with @LarnuUK here. In a past job, I was so convinced by the context-manager behaviour of Python that I persuaded my whole team to do a code review and change all of their connections to use a context manager. These idle connections linger for no purpose at all that I can see. The server will clear them (over long periods of time), but the more software that's running over the DB, the more they start to accumulate. Depending on the RDBMS you're using, it's when you hit ~500 that things will go south. In this case, my decision ended up crashing our central Redshift cluster and brought down our services for more than 10 clients and launched a major issue. As mentioned previously; |
Thanks @LarnuUK & @roganjoshp for your invaluable feedback and comments. This PR is still under review and our team was still investigating these points in detail. Having said that, we will close connection automatically upon |
Work Item / Issue Reference
Summary
This pull request introduces context manager support for the
Connection
andCursor
classes in themssql_python
module, 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 theConnection
class to enable usage with thewith
statement. The connection commits transactions on normal exit and rolls back on exceptions, without closing the connection. [1] [2]__enter__
and__exit__
methods to theCursor
class to allow usage with thewith
statement. The cursor commits transactions on normal exit but does not close itself, aligning withpyodbc
behavior.Logging Enhancements
Connection.close
method to log when uncommitted changes are rolled back before closing.Test Coverage
tests/test_003_connection.py
to verify context manager behavior for connections, including:contextlib.closing
with connections to ensure proper closure after context exit.tests/test_004_cursor.py
to includecontextlib.closing
for cursor tests.