Skip to content

With + Connection - returns cursor rather than the connection, doesn't close connection on exit #735

Closed
@greghoop

Description

@greghoop

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:

def __enter__(self):

def __exit__(self, exc, value, traceback):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions