Reputation: 81
I am using create_engine command from sqlalchemy library to dump dataframe into database.
Please see below code:
from sqlalchemy import create_engine
connection = create_engine(.....)
DataFrame.to_sql(..., connection)
Do I need to do this connection.close() at the end?
Upvotes: 0
Views: 62
Reputation: 25966
You incorrectly assume that the result of createEngine
is a connection. See Engine description
Connects a Pool and Dialect together to provide a source of database connectivity and behavior.
See also Working with Engines and Connections
The typical usage of create_engine() is once per particular database URL, held globally for the lifetime of a single application process. A single Engine manages many individual DBAPI connections on behalf of the process and is intended to be called upon in a concurrent fashion. The Engine is not synonymous to the DBAPI connect function, which represents just one connection resource - the Engine is most efficient when created just once at the module level of an application, not per-object or per-function call.
You can close connections managed by the engine with dispose
Dispose of the connection pool used by this Engine.
This has the effect of fully closing all currently checked in database connections. Connections that are still checked out will not be closed, however they will no longer be associated with this Engine, so when they are closed individually, eventually the Pool which they are associated with will be garbage collected and they will be closed out fully, if not already closed on checkin.
Upvotes: 1