diff options
Diffstat (limited to 'django/db/backends/__init__.py')
| -rw-r--r-- | django/db/backends/__init__.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 0f9423c1c3..2a7a206c3b 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -352,6 +352,18 @@ class BaseDatabaseWrapper(object): def make_debug_cursor(self, cursor): return util.CursorDebugWrapper(cursor, self) + @contextmanager + def temporary_connection(self): + # Ensure a connection is established, and avoid leaving a dangling + # connection, for operations outside of the request-response cycle. + must_close = self.connection is None + cursor = self.cursor() + try: + yield + finally: + cursor.close() + if must_close: + self.close() class BaseDatabaseFeatures(object): allows_group_by_pk = False |
