Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.3.x] Fixed #18135 -- Close connection used for db version checking

On MySQL when checking the server version, a new connection could be
created but never closed. This could result in open connections on
server startup.

Backport of 4423757.
  • Loading branch information...
commit a15d3b58d8c4cbb6137f0458544ec03f3394bb08 1 parent e293d82
Michael Newman authored May 27, 2012 akaariai committed May 27, 2012
15  django/db/backends/mysql/base.py
@@ -334,10 +334,19 @@ def _rollback(self):
334 334
 
335 335
     def get_server_version(self):
336 336
         if not self.server_version:
  337
+            new_connection = False
337 338
             if not self._valid_connection():
338  
-                self.cursor()
339  
-            m = server_version_re.match(self.connection.get_server_info())
  339
+                # Ensure we have a connection with the DB by using a temporary
  340
+                # cursor
  341
+                new_connection = True
  342
+                self.cursor().close()
  343
+            server_info = self.connection.get_server_info()
  344
+            if new_connection:
  345
+                # Make sure we close the connection
  346
+                self.connection.close()
  347
+                self.connection = None
  348
+            m = server_version_re.match(server_info)
340 349
             if not m:
341  
-                raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
  350
+                raise Exception('Unable to determine MySQL version from version string %r' % server_info)
342 351
             self.server_version = tuple([int(x) for x in m.groups()])
343 352
         return self.server_version
8  tests/regressiontests/backends/tests.py
@@ -56,6 +56,14 @@ def test_client_encoding(self):
56 56
         self.assertEqual(connection.connection.encoding, "UTF-8")
57 57
         self.assertEqual(connection.connection.nencoding, "UTF-8")
58 58
 
  59
+class MySQLTests(TestCase):
  60
+    @unittest.skipUnless(connection.vendor == 'mysql',
  61
+                        "Test valid only for MySQL")
  62
+    def test_server_version_connections(self):
  63
+        connection.close()
  64
+        connection.get_server_version()
  65
+        self.assertTrue(connection.connection is None)
  66
+
59 67
 class DateQuotingTest(TestCase):
60 68
 
61 69
     def test_django_date_trunc(self):

0 notes on commit a15d3b5

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