diff options
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/engine/base.py | 16 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/default.py | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/result.py | 7 |
3 files changed, 19 insertions, 12 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index f680edada..91f4493c2 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -1203,14 +1203,22 @@ class Connection(Connectable): else: result = context.get_result_proxy() if result._metadata is None: - result._soft_close(_autoclose_connection=False) + result._soft_close() if context.should_autocommit and self._root.__transaction is None: self._root._commit_impl(autocommit=True) - if result._soft_closed and self.should_close_with_result: - self.close() - + # for "connectionless" execution, we have to close this + # Connection after the statement is complete. + if self.should_close_with_result: + # ResultProxy already exhausted rows / has no rows. + # close us now + if result._soft_closed: + self.close() + else: + # ResultProxy will close this Connection when no more + # rows to fetch. + result._autoclose_connection = True return result def _cursor_execute(self, cursor, statement, parameters, context=None): diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 1c10f484f..628e23c9e 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -939,15 +939,15 @@ class DefaultExecutionContext(interfaces.ExecutionContext): row = result.fetchone() self.returned_defaults = row self._setup_ins_pk_from_implicit_returning(row) - result._soft_close(_autoclose_connection=False) + result._soft_close() result._metadata = None elif not self._is_explicit_returning: - result._soft_close(_autoclose_connection=False) + result._soft_close() result._metadata = None elif self.isupdate and self._is_implicit_returning: row = result.fetchone() self.returned_defaults = row - result._soft_close(_autoclose_connection=False) + result._soft_close() result._metadata = None elif result._metadata is None: @@ -955,7 +955,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext): # (which requires open cursor on some drivers # such as kintersbasdb, mxodbc) result.rowcount - result._soft_close(_autoclose_connection=False) + result._soft_close() return result def _setup_ins_pk_from_lastrowid(self): diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index 903b2c2f9..907dc7bd2 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -638,7 +638,7 @@ class ResultProxy(object): _process_row = RowProxy out_parameters = None - _can_close_connection = False + _autoclose_connection = False _metadata = None _soft_closed = False closed = False @@ -792,7 +792,7 @@ class ResultProxy(object): return self._saved_cursor.description - def _soft_close(self, _autoclose_connection=True): + def _soft_close(self): """Soft close this :class:`.ResultProxy`. This releases all DBAPI cursor resources, but leaves the @@ -820,8 +820,7 @@ class ResultProxy(object): self._soft_closed = True cursor = self.cursor self.connection._safe_close_cursor(cursor) - if _autoclose_connection and \ - self.connection.should_close_with_result: + if self._autoclose_connection: self.connection.close() self.cursor = None |