diff --git a/target_postgres/connector.py b/target_postgres/connector.py index 3bc34e87..5452b7c9 100644 --- a/target_postgres/connector.py +++ b/target_postgres/connector.py @@ -180,8 +180,10 @@ def copy_table_structure( @contextmanager def _connect(self) -> t.Iterator[sqlalchemy.engine.Connection]: - with self._engine.connect().execution_options() as conn: + engine = self._engine + with engine.connect().execution_options() as conn: yield conn + engine.dispose() def drop_table( self, table: sqlalchemy.Table, connection: sqlalchemy.engine.Connection @@ -800,9 +802,7 @@ def column_exists( # type: ignore[override] ) def __del__(self): - """ - Dispose underlying SQLAlchemy engine object. - """ + """Dispose underlying SQLAlchemy engine object.""" if self._cached_engine is not None: self._cached_engine.dispose()