summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-03-11 21:08:49 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-03-11 21:08:49 +0100
commitf2f98abb955c83eca3ceea62d1711c80ae028d33 (patch)
tree2613eacb049f985d001d73ca2e60a528f3ec84c5
parentb746f8a9e3fb22fe5ff58bd2bd8b20fa80cd4800 (diff)
Avoided closing the database connection within a transaction.
Refs #9437.
-rw-r--r--django/contrib/gis/db/backends/postgis/operations.py16
-rw-r--r--django/db/backends/__init__.py4
2 files changed, 7 insertions, 13 deletions
diff --git a/django/contrib/gis/db/backends/postgis/operations.py b/django/contrib/gis/db/backends/postgis/operations.py
index fe90343411..4d31bbb53c 100644
--- a/django/contrib/gis/db/backends/postgis/operations.py
+++ b/django/contrib/gis/db/backends/postgis/operations.py
@@ -402,18 +402,10 @@ class PostGISOperations(DatabaseOperations, BaseSpatialOperations):
"""
Helper routine for calling PostGIS functions and returning their result.
"""
- cursor = self.connection._cursor()
- try:
- try:
- cursor.execute('SELECT %s()' % func)
- row = cursor.fetchone()
- except:
- # Responsibility of callers to perform error handling.
- raise
- finally:
- # Close out the connection. See #9437.
- self.connection.close()
- return row[0]
+ # Close out the connection. See #9437.
+ with self.connection.temporary_connection() as cursor:
+ cursor.execute('SELECT %s()' % func)
+ return cursor.fetchone()[0]
def postgis_geos_version(self):
"Returns the version of the GEOS library used with PostGIS."
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 09a33eebae..c797a50733 100644
--- a/django/db/backends/__init__.py
+++ b/django/db/backends/__init__.py
@@ -472,11 +472,13 @@ class BaseDatabaseWrapper(object):
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.
+
+ Provides a cursor: with self.temporary_connection() as cursor: ...
"""
must_close = self.connection is None
cursor = self.cursor()
try:
- yield
+ yield cursor
finally:
cursor.close()
if must_close: