Refs #16614 -- Prevented database errors from being masked by cursor close.

When an error occurred during the cursor.execute statement, the cursor
is closed. This operation did not fail with client-side cursors. Now,
with server-side cursors, the close operation might fail (example
below). The original error should be raised, not the one raised by
cursor.close(), this is only clean-up code.

For example, one can attempt to create a named cursor for an invalid
query. psycopg will raise an error about the invalid query and the
server-side cursor will not be created on PostgreSQL. When the code
attempts to cursor.close(), it asks psycopg to close a cursor that was
not created. pyscopg raises a new error: psycopg2.OperationalError:
cursor "_django_curs_140365867840512_20" does not exist.
This commit is contained in:
François Freitag 2017-01-13 15:21:33 -08:00 committed by Tim Graham
parent 2e55790838
commit 6b6be692fc
1 changed files with 7 additions and 2 deletions

View File

@ -830,9 +830,14 @@ class SQLCompiler(object):
try:
cursor.execute(sql, params)
except Exception:
with self.connection.wrap_database_errors:
# Closing a server-side cursor could yield an error
try:
# Might fail for server-side cursors (e.g. connection closed)
cursor.close()
except Exception:
# Ignore clean up errors and raise the original error instead.
# Python 2 doesn't chain exceptions. Remove this error
# silencing when dropping Python 2 compatibility.
pass
raise
if result_type == CURSOR: