15

With Pandas, I can very easily read data from a database into a dataframe:

from sqlalchemy import create_engine
import pandas


query = 'SELECT * FROM Table_Name;'
engine = create_engine('...')

df = pandas.read_sql_query(query, engine)

print(df.head())

I would like to make sure that no connection is kept open after executing .read_sql_query(), no matters if the query succeeded or if it raised an exception.

I am currently:

  • Using a function to restrict the engine's scope. I only expect to call this function once each half an hour, so I do not mind re-creating the engine if that helps ensuring everything is cleaned/closed/garbage-collected.
  • Disabling pooling with poolclass=NullPool.
  • Finally calling engine.disponse().

Like so:

from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
import pandas


def get_data():
    query = 'SELECT * FROM Table_Name;'
    try:
        engine = create_engine('...', poolclass=NullPool)
        df = pandas.read_sql_query(query, engine)
    finally:
        engine.dispose()
    return df


print(get_data().head())

Is there a better way?

YaOzI
  • 10,391
  • 4
  • 59
  • 62
Peque
  • 10,375
  • 7
  • 48
  • 83

1 Answers1

24

Backgrounds:

When using sqlalchemy with pandas read_sql_query(query, con) method, it will create a SQLDatabase object with an attribute connectable to self.connectable.execute(query). And the SQLDatabase.connectable is initialized as con as long as it is an instance of sqlalchemy.engine.Connectable (i.e. Engine and Connection).

Case I: when passing Engine object as con

Just as example code in your question:

from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('...')
df = pd.read_sql_query(query, con=engine)

Internally, pandas just use result = engine.execute(query), which means:

Where above, the execute() method acquires a new Connection on its own, executes the statement with that object, and returns the ResultProxy. In this case, the ResultProxy contains a special flag known as close_with_result, which indicates that when its underlying DBAPI cursor is closed, the Connection object itself is also closed, which again returns the DBAPI connection to the connection pool, releasing transactional resources.

In this case, you don't have to worry about the Connection itself, which is closed automatically, but it will keep the connection pool of engine.

So you can either disable pooling by using:

engine = create_engine('...', poolclass=NullPool)

or dispose the engine entirely with engine.dispose() at the end.

But following the Engine Disposal doc (the last paragraph), these two are alternative, you don't have to use them at the same time. So in this case, for simple one-time usage of read_sql_query and clean-up, I think this should be enough:

# Clean up entirely after every query.
engine = create_engine('...')
df = pd.read_sql_query(query, con=engine)
engine.dispose()

Case II: when passing Connection object as con:

connection = engine.connect()
print(connection.closed) # False
df = pd.read_sql_query(query, con=connection)
print(connection.closed) # False again
# do_something_else(connection)
connection.close()
print(connection.closed) # True
engine.dispose()

You should do this whenever you want greater control over attributes of the connection, when it gets closed, etc. For example, a very import example of this is a Transaction, which lets you decide when to commit your changes to the database. (from this answer)

But with pandas, we have no control inside the read_sql_query, the only usefulness of connection is that it allows you to do more useful things before we explicitly close it.


So generally speaking:

I think I would like to use following pattern, which gives me more control of connections and leaves the future extensibility:

engine = create_engine('...')
# Context manager makes sure the `Connection` is closed safely and implicitly
with engine.connect() as conn:
    df = pd.read_sql_query(query, conn)
    print(conn.in_transaction()) # False
    # do_something_with(conn)
    trans = conn.begin()
    print(conn.in_transaction()) # True
    # do_whatever_with(trans)
    print(conn.closed) # False
print('Is Connection with-OUT closed?', conn.closed) # True
engine.dispose()

But for simple usage cases such as your example code, I think both ways are equally clean and simple for clean-up DB IO resources.

YaOzI
  • 10,391
  • 4
  • 59
  • 62
  • I see you create a connection named `conn`, but you do not use it to read the dataframe, you pass the engine as parameter to `read_sql_query()`. How would that help then? It seems the context manager is mananing a connection that is never used. Or am I missing something? – Peque Jul 09 '18 at 10:50
  • @Peque Sorry, that's a typo. And actually passing `engine` will also works, but that's different. Check out there differences [here](https://stackoverflow.com/q/34322471/2303761). – YaOzI Jul 09 '18 at 10:56
  • @Peque I think there might be some confusion when combining the SQLAlchemy context management with pandas SQL IO usage, so after some more digging, I rewrite my answer entirely. Hope that can help, and correct me if I am wrong :) – YaOzI Jul 09 '18 at 14:43
  • Thanks for your detailed answer. :-) – Peque Jul 09 '18 at 15:47