Permalink
Browse files

[1.4.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...
newmaniese authored and akaariai committed May 27, 2012
1 parent d3fa8d9 commit 0f69a16785b88d03dc246a652cf348e0f2704e8e
Showing with 20 additions and 3 deletions.
  1. +12 −3 django/db/backends/mysql/base.py
  2. +8 −0 tests/regressiontests/backends/tests.py
@@ -407,11 +407,20 @@ def _rollback(self):
def get_server_version(self):
if not self.server_version:
+ new_connection = False
if not self._valid_connection():
- self.cursor()
- m = server_version_re.match(self.connection.get_server_info())
+ # Ensure we have a connection with the DB by using a temporary
+ # cursor
+ new_connection = True
+ self.cursor().close()
+ server_info = self.connection.get_server_info()
+ if new_connection:
+ # Make sure we close the connection
+ self.connection.close()
+ self.connection = None
+ m = server_version_re.match(server_info)
if not m:
- raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
+ raise Exception('Unable to determine MySQL version from version string %r' % server_info)
self.server_version = tuple([int(x) for x in m.groups()])
return self.server_version
@@ -65,6 +65,14 @@ def test_client_encoding(self):
self.assertEqual(connection.connection.encoding, "UTF-8")
self.assertEqual(connection.connection.nencoding, "UTF-8")
+class MySQLTests(TestCase):
+ @unittest.skipUnless(connection.vendor == 'mysql',
+ "Test valid only for MySQL")
+ def test_server_version_connections(self):
+ connection.close()
+ connection.get_server_version()
+ self.assertTrue(connection.connection is None)
+
class DateQuotingTest(TestCase):
def test_django_date_trunc(self):

0 comments on commit 0f69a16

Please sign in to comment.