Code

Ticket #18135: 18135-sleeping-mysql-connection-1.4.X.diff

File 18135-sleeping-mysql-connection-1.4.X.diff, 1.2 KB (added by ramiro, 2 years ago)

Patch for this issue in 1.4.X branch

Line 
1diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
2--- a/django/db/backends/mysql/base.py
3+++ b/django/db/backends/mysql/base.py
4@@ -407,11 +407,20 @@
5 
6     def get_server_version(self):
7         if not self.server_version:
8+            new_connection = False
9             if not self._valid_connection():
10-                self.cursor()
11-            m = server_version_re.match(self.connection.get_server_info())
12+                # Ensure we have a connection with the DB by using a temporary
13+                # cursor
14+                new_connection = True
15+                self.cursor().close()
16+            server_info = self.connection.get_server_info()
17+            if new_connection:
18+                # Make sure we close the connection
19+                self.connection.close()
20+                self.connection = None
21+            m = server_version_re.match(server_info)
22             if not m:
23-                raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
24+                raise Exception('Unable to determine MySQL version from version string %r' % server_info)
25             self.server_version = tuple([int(x) for x in m.groups()])
26         return self.server_version
27