-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
With + Connection - returns cursor rather than the connection, doesn't close connection on exit #735
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
Comments
These APIs are added for compatibility with mysqlclient. But I forgot to deprecate these API in PyMySQL. |
Glad to know I'm not alone in seeing this, and +1 to @greghoop link to https://stackoverflow.com/questions/31214658/can-i-use-pymysql-connect-with-with-statement. The context manager is a good interim workaround if you do not want to assign the connection to a variable manually. |
I also got this strange issue, will this be solved soon? |
By returning a cursor rather than a connection, extra code needs to be written to handle exceptions and close the connection. Why not return a connection object, then a user can use a second nested with to get a cursor object. This way, wherever an error occurs, the cursors/connections will be neatly closed, and tracebacks will nicely propagate.
I noticed this was a problem when using SSH, and the tunnel wasn't getting decommissioned because the DB connection was still active. Other users also having this problem:
https://stackoverflow.com/questions/31214658/can-i-use-pymysql-connect-with-with-statement
Relevant lines in the code:
PyMySQL/pymysql/connections.py
Line 494 in 09040ab
PyMySQL/pymysql/connections.py
Line 498 in 09040ab
The text was updated successfully, but these errors were encountered: