Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1442 -- Fixed multithreading problem with various database bac…

…kends. Thanks, Eugene Lazutkin

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2579 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 67cbb5c248706bc101a6f3b9d65643423b5df1f8 1 parent 3ff5b99
Adrian Holovaty authored March 28, 2006
9  django/core/db/backends/ado_mssql.py
@@ -44,7 +44,14 @@ def variantToPython(variant, adType):
44 44
     return res
45 45
 Database.convertVariantToPython = variantToPython
46 46
 
47  
-class DatabaseWrapper:
  47
+try:
  48
+    # Only exists in python 2.4+
  49
+    from threading import local
  50
+except ImportError:
  51
+    # Import copy of _thread_local.py from python 2.4
  52
+    from django.utils._threading_local import local
  53
+
  54
+class DatabaseWrapper(local):
48 55
     def __init__(self):
49 56
         self.connection = None
50 57
         self.queries = []
21  django/core/db/backends/mysql.py
@@ -47,14 +47,31 @@ def __getattr__(self, attr):
47 47
         else:
48 48
             return getattr(self.cursor, attr)
49 49
 
50  
-class DatabaseWrapper:
  50
+try:
  51
+    # Only exists in python 2.4+
  52
+    from threading import local
  53
+except ImportError:
  54
+    # Import copy of _thread_local.py from python 2.4
  55
+    from django.utils._threading_local import local
  56
+
  57
+class DatabaseWrapper(local):
51 58
     def __init__(self):
52 59
         self.connection = None
53 60
         self.queries = []
54 61
 
  62
+    def _valid_connection(self):
  63
+        if self.connection is not None:
  64
+            try:
  65
+                self.connection.ping()
  66
+                return True
  67
+            except DatabaseError:
  68
+                self.connection.close()
  69
+                self.connection = None
  70
+        return False
  71
+
55 72
     def cursor(self):
56 73
         from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG
57  
-        if self.connection is None:
  74
+        if not self._valid_connection():
58 75
             kwargs = {
59 76
                 'user': DATABASE_USER,
60 77
                 'db': DATABASE_NAME,
9  django/core/db/backends/postgresql.py
@@ -9,7 +9,14 @@
9 9
 
10 10
 DatabaseError = Database.DatabaseError
11 11
 
12  
-class DatabaseWrapper:
  12
+try:
  13
+    # Only exists in python 2.4+
  14
+    from threading import local
  15
+except ImportError:
  16
+    # Import copy of _thread_local.py from python 2.4
  17
+    from django.utils._threading_local import local
  18
+
  19
+class DatabaseWrapper(local):
13 20
     def __init__(self):
14 21
         self.connection = None
15 22
         self.queries = []
9  django/core/db/backends/sqlite3.py
@@ -24,7 +24,14 @@ def utf8(s):
24 24
             return s
25 25
     return [utf8(r) for r in row]
26 26
 
27  
-class DatabaseWrapper:
  27
+try:
  28
+    # Only exists in python 2.4+
  29
+    from threading import local
  30
+except ImportError:
  31
+    # Import copy of _thread_local.py from python 2.4
  32
+    from django.utils._threading_local import local
  33
+
  34
+class DatabaseWrapper(local):
28 35
     def __init__(self):
29 36
         self.connection = None
30 37
         self.queries = []

0 notes on commit 67cbb5c

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