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.
This commit is contained in:
Michael Newman 2012-05-27 18:24:35 +03:00 committed by Anssi Kääriäinen
parent a8a81aae20
commit 4423757c0c
2 changed files with 17 additions and 2 deletions

View File

@ -418,11 +418,20 @@ class DatabaseWrapper(BaseDatabaseWrapper):
@cached_property @cached_property
def mysql_version(self): def mysql_version(self):
if not self.server_version: if not self.server_version:
new_connection = False
if not self._valid_connection(): if not self._valid_connection():
# Ensure we have a connection with the DB by using a temporary
# cursor
new_connection = True
self.cursor().close() self.cursor().close()
m = server_version_re.match(self.connection.get_server_info()) 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: 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()]) self.server_version = tuple([int(x) for x in m.groups()])
return self.server_version return self.server_version

View File

@ -89,6 +89,12 @@ class MySQLTests(TestCase):
else: else:
self.assertFalse(found_reset) self.assertFalse(found_reset)
@unittest.skipUnless(connection.vendor == 'mysql',
"Test valid only for MySQL")
def test_server_version_connections(self):
connection.close()
connection.mysql_version
self.assertTrue(connection.connection is None)
class DateQuotingTest(TestCase): class DateQuotingTest(TestCase):