From a15d3b58d8c4cbb6137f0458544ec03f3394bb08 Mon Sep 17 00:00:00 2001 From: Michael Newman Date: Sun, 27 May 2012 21:51:03 +0300 Subject: [PATCH] [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 4423757c0c50afbe2470434778c8d5e5b4a70925. --- django/db/backends/mysql/base.py | 15 ++++++++++++--- tests/regressiontests/backends/tests.py | 8 ++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 98233c7329..55f3c3d71c 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -334,10 +334,19 @@ class DatabaseWrapper(BaseDatabaseWrapper): 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 diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index fc2a973ab1..ee2e15bbdd 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -56,6 +56,14 @@ class OracleChecks(unittest.TestCase): 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):