Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Avoided closing the database connection within a transaction.

Refs #9437.
  • Loading branch information...
commit f2f98abb955c83eca3ceea62d1711c80ae028d33 1 parent b746f8a
Aymeric Augustin authored March 11, 2013
16  django/contrib/gis/db/backends/postgis/operations.py
@@ -402,18 +402,10 @@ def _get_postgis_func(self, func):
402 402
         """
403 403
         Helper routine for calling PostGIS functions and returning their result.
404 404
         """
405  
-        cursor = self.connection._cursor()
406  
-        try:
407  
-            try:
408  
-                cursor.execute('SELECT %s()' % func)
409  
-                row = cursor.fetchone()
410  
-            except:
411  
-                # Responsibility of callers to perform error handling.
412  
-                raise
413  
-        finally:
414  
-            # Close out the connection.  See #9437.
415  
-            self.connection.close()
416  
-        return row[0]
  405
+        # Close out the connection.  See #9437.
  406
+        with self.connection.temporary_connection() as cursor:
  407
+            cursor.execute('SELECT %s()' % func)
  408
+            return cursor.fetchone()[0]
417 409
 
418 410
     def postgis_geos_version(self):
419 411
         "Returns the version of the GEOS library used with PostGIS."
4  django/db/backends/__init__.py
@@ -472,11 +472,13 @@ def temporary_connection(self):
472 472
         Context manager that ensures that a connection is established, and
473 473
         if it opened one, closes it to avoid leaving a dangling connection.
474 474
         This is useful for operations outside of the request-response cycle.
  475
+
  476
+        Provides a cursor: with self.temporary_connection() as cursor: ...
475 477
         """
476 478
         must_close = self.connection is None
477 479
         cursor = self.cursor()
478 480
         try:
479  
-            yield
  481
+            yield cursor
480 482
         finally:
481 483
             cursor.close()
482 484
             if must_close:

0 notes on commit f2f98ab

Please sign in to comment.
Something went wrong with that request. Please try again.