From 9609f5ffb52ce8a4969059e299773ac7176dbb0d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 3 Apr 2017 17:25:26 -0400 Subject: ResultProxy won't autoclose connection until state flag is set Changed the mechanics of :class:`.ResultProxy` to unconditionally delay the "autoclose" step until the :class:`.Connection` is done with the object; in the case where Postgresql ON CONFLICT with RETURNING returns no rows, autoclose was occurring in this previously non-existent use case, causing the usual autocommit behavior that occurs unconditionally upon INSERT/UPDATE/DELETE to fail. Change-Id: I235a25daf4381b31f523331f810ea04450349722 Fixes: #3955 (cherry picked from commit 8ee363e4917b0dcd64a83b6d26e465c9e61e0ea5) (cherry picked from commit f52fb5282a046d26b6ee2778e03b995eb117c2ee) --- lib/sqlalchemy/engine/base.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'lib/sqlalchemy/engine/base.py') 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): -- cgit v1.2.1