Skip to content

DOC: pd.read_sql() connection disposal for str type connections #35495

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
AdamSpannbauer opened this issue Jul 31, 2020 · 1 comment · Fixed by #35503
Closed

DOC: pd.read_sql() connection disposal for str type connections #35495

AdamSpannbauer opened this issue Jul 31, 2020 · 1 comment · Fixed by #35503
Labels

Comments

@AdamSpannbauer
Copy link
Contributor

This is in between a documentation issue and a general question.

Location of the documentation

pd.read_sql()

Documentation problem

The documentation notes that if the con is a SQLAlchemy connectable that the user is responsible for disposing/closing. The documentation doesn't note whether the connection is closed if the user provides a str to the con parameter. In my quick search, I couldn't find a reference to .dispose()/.close() in the source code when the user provides a str type con. Is this connection being closed? Is it being left up to garbage collection as noted in SQLAlchemy docs?

Suggested fix for documentation

I think a note about connection closure for str type cons would be a nice addition. Right now, if I'm understanding correctly, the connection is being garbage collected and the user needs to infer from the docs that they don't need to worry about an open connection.

Current wording:

The user is responsible for engine disposal and connection closure for the SQLAlchemy connectable.

Potential wording:

The user is responsible for engine disposal and connection closure for the SQLAlchemy connectable; str connections are closed automatically.

@AdamSpannbauer AdamSpannbauer added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 31, 2020
@AdamSpannbauer AdamSpannbauer changed the title DOC: DOC: pd.read_sql() connection disposal for str type connections Jul 31, 2020
@arw2019
Copy link
Member

arw2019 commented Jul 31, 2020

I think that's right. If I didn't make a mistake at the end of the day we connect to the database here:

pandas/pandas/io/sql.py

Lines 1159 to 1163 in bdcc5bf

def execute(self, *args, **kwargs):
"""Simple passthrough to SQLAlchemy connectable"""
return self.connectable.execution_options(no_parameters=True).execute(
*args, **kwargs
)

and scrolling down the SQLAlchemy doc page you linked, at the bottom of theConnectionless Execution, Implicit Execution section:

the Connection is created behind the scenes; the CursorResult returned by the execute() call references the Connection 
used to issue the SQL statement. When the CursorResult is closed, the underlying Connection is closed for us, 
resulting in the DBAPI connection being returned to the pool with transactional resources removed.

A pull request with the clarification you propose will be welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants