MCPcopy
hub / github.com/django/django / temporary_connection

Method temporary_connection

django/db/backends/base/base.py:685–699  ·  view source on GitHub ↗

Context manager that ensures that a connection is established, and if it opened one, closes it to avoid leaving a dangling connection. This is useful for operations outside of the request-response cycle. Provide a cursor: with self.temporary_connection() as cursor:

(self)

Source from the content-addressed store, hash-verified

683
684 @contextmanager
685 def temporary_connection(self):
686 """
687 Context manager that ensures that a connection is established, and
688 if it opened one, closes it to avoid leaving a dangling connection.
689 This is useful for operations outside of the request-response cycle.
690
691 Provide a cursor: with self.temporary_connection() as cursor: ...
692 """
693 must_close = self.connection is None
694 try:
695 with self.cursor() as cursor:
696 yield cursor
697 finally:
698 if must_close:
699 self.close()
700
701 @contextmanager
702 def _nodb_cursor(self):

Callers 6

_get_postgis_funcMethod · 0.80
pg_versionMethod · 0.80
oracle_versionMethod · 0.80
mysql_server_dataMethod · 0.80

Calls 2

cursorMethod · 0.95
closeMethod · 0.95