Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Ensured a connection is established when checking the database version.

Fixed a test broken by 21765c0. Refs #18135.
  • Loading branch information...
commit ebabd772911f732ef54e014f130f6f5530198e14 1 parent 9a3988c
Aymeric Augustin authored February 19, 2013
12  django/db/backends/__init__.py
@@ -352,6 +352,18 @@ def cursor(self):
352 352
     def make_debug_cursor(self, cursor):
353 353
         return util.CursorDebugWrapper(cursor, self)
354 354
 
  355
+    @contextmanager
  356
+    def temporary_connection(self):
  357
+        # Ensure a connection is established, and avoid leaving a dangling
  358
+        # connection, for operations outside of the request-response cycle.
  359
+        must_close = self.connection is None
  360
+        cursor = self.cursor()
  361
+        try:
  362
+            yield
  363
+        finally:
  364
+            cursor.close()
  365
+            if must_close:
  366
+                self.close()
355 367
 
356 368
 class BaseDatabaseFeatures(object):
357 369
     allows_group_by_pk = False
3  django/db/backends/mysql/base.py
@@ -453,7 +453,8 @@ def _rollback(self):
453 453
 
454 454
     @cached_property
455 455
     def mysql_version(self):
456  
-        server_info = self.connection.get_server_info()
  456
+        with self.temporary_connection():
  457
+            server_info = self.connection.get_server_info()
457 458
         match = server_version_re.match(server_info)
458 459
         if not match:
459 460
             raise Exception('Unable to determine MySQL version from version string %r' % server_info)
4  django/db/backends/oracle/base.py
@@ -623,8 +623,10 @@ def _commit(self):
623 623
 
624 624
     @cached_property
625 625
     def oracle_version(self):
  626
+        with self.temporary_connection():
  627
+            version = self.connection.version
626 628
         try:
627  
-            return int(self.connection.version.split('.')[0])
  629
+            return int(version.split('.')[0])
628 630
         except ValueError:
629 631
             return None
630 632
 
3  django/db/backends/postgresql_psycopg2/base.py
@@ -152,7 +152,8 @@ def close(self):
152 152
 
153 153
     @cached_property
154 154
     def pg_version(self):
155  
-        return get_version(self.connection)
  155
+        with self.temporary_connection():
  156
+            return get_version(self.connection)
156 157
 
157 158
     def get_connection_params(self):
158 159
         settings_dict = self.settings_dict
3  django/db/backends/postgresql_psycopg2/operations.py
@@ -195,8 +195,7 @@ def check_aggregate_support(self, aggregate):
195 195
         NotImplementedError if this is the database in use.
196 196
         """
197 197
         if aggregate.sql_function in ('STDDEV_POP', 'VAR_POP'):
198  
-            pg_version = self.connection.pg_version
199  
-            if pg_version >= 80200 and pg_version <= 80204:
  198
+            if 80200 <= self.connection.pg_version <= 80204:
200 199
                 raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
201 200
 
202 201
     def max_name_length(self):

0 notes on commit ebabd77

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