Skip to content

No self.close() in __exit__ method of connection? #396

Closed
@ghost

Description

Is it possible (or sensible) to use the context handler for the connection, e.g.:

connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
with connection as cursor:
    ... # do something
# proceed

Precisely: Is the connection closed after the with-block? I ask, because

  1. the example given on https://github.com/PyMySQL/PyMySQL doesn't use the context handler for the connection but for the cursor;
  2. the exit method of the connection object is
def __exit__(self, exc, value, traceback):
    """On successful exit, commit. On exception, rollback"""
    if exc:
        self.rollback()
    else:
        self.commit()

and not

def __exit__(self, exc, value, traceback):
    """On successful exit, commit. On exception, rollback"""
    if exc:
        self.rollback()
    else:
        self.commit()
    self.close()

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