diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 9433540d7c..6892f95617 100644
|
a
|
b
|
class SQLCompiler:
|
| 1138 | 1138 | cursor = self.connection.cursor() |
| 1139 | 1139 | try: |
| 1140 | 1140 | cursor.execute(sql, params) |
| 1141 | | except Exception: |
| | 1141 | except Exception as e: |
| 1142 | 1142 | # Might fail for server-side cursors (e.g. connection closed) |
| 1143 | | cursor.close() |
| 1144 | | raise |
| | 1143 | try: |
| | 1144 | cursor.close() |
| | 1145 | except Exception: |
| | 1146 | # If we got an error creating the cursor, then closing it |
| | 1147 | # will always fail. Raise the outer exception instead of the |
| | 1148 | # obscure "cursor _django_curs_xxxx does not exist". |
| | 1149 | raise e from None |
| | 1150 | else: |
| | 1151 | raise e |
| 1145 | 1152 | |
| 1146 | 1153 | if result_type == CURSOR: |
| 1147 | 1154 | # Give the caller the cursor to process and close. |