Skip to content

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

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

Closed
ghost opened this issue Dec 3, 2015 · 1 comment
Closed

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

ghost opened this issue Dec 3, 2015 · 1 comment

Comments

@ghost
Copy link

ghost commented Dec 3, 2015

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()
@methane
Copy link
Member

methane commented Dec 3, 2015

Is it possible (or sensible) to use the context handler for the connection

No. DB-API 2.0 doesn't define context manager API.

Precisely: Is the connection closed after the with-block?

No, as you can see.

Currently, connection's contextmanager is for managing transaction.
For compatibility with MySQL-python, I can't change the behavior easily.

@methane methane closed this as completed Dec 3, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant