Ticket #463: mysql_trunk_rev2360-1.diff
File mysql_trunk_rev2360-1.diff, 3.7 KB (added by , 19 years ago) |
---|
-
django/core/db/backends/mysql.py
10 10 from MySQLdb.converters import conversions 11 11 from MySQLdb.constants import FIELD_TYPE 12 12 import types 13 import threading 13 14 14 15 DatabaseError = Database.DatabaseError 15 16 … … 47 48 else: 48 49 return getattr(self.cursor, attr) 49 50 51 class ThreadLocal: 52 def __getattr__(self, key): 53 threadkey = 'ThreadLocal_%s' % (id(self),) 54 thread = threading.currentThread() 55 try: 56 local = getattr(thread, threadkey) 57 return local[key] 58 except AttributeError: 59 raise AttributeError(key) 60 except KeyError: 61 raise AttributeError(key) 62 63 def __setattr__(self, key, value): 64 threadkey = 'ThreadLocal_%s' % (id(self),) 65 thread = threading.currentThread() 66 try: 67 local = getattr(thread, threadkey) 68 except AttributeError: 69 local = {} 70 setattr(thread, threadkey, local) 71 local[key] = value 72 73 def __delattr__(self, key): 74 threadkey = 'ThreadLocal_%s' % (id(self),) 75 thread = threading.currentThread() 76 try: 77 local = getattr(thread, threadkey) 78 del local[key] 79 except AttributeError: 80 raise AttributeError(key) 81 except KeyError: 82 raise AttributeError(key) 83 50 84 class DatabaseWrapper: 51 85 def __init__(self): 52 self.connection = None53 86 self.queries = [] 87 if hasattr(threading, 'local'): 88 # threading.local is available in Python 2.4 and later 89 self.threadlocal = threading.local() 90 else: 91 # workaround for Python 2.3 and earlier 92 self.threadlocal = ThreadLocal() 54 93 55 94 def cursor(self): 56 95 from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG 57 if self.connection is None: 96 conn = getattr(self.threadlocal, 'connection', None) 97 if conn is None: 58 98 kwargs = { 59 99 'user': DATABASE_USER, 60 100 'db': DATABASE_NAME, … … 64 104 } 65 105 if DATABASE_PORT: 66 106 kwargs['port'] = DATABASE_PORT 67 self.connection = Database.connect(**kwargs) 68 cursor = self.connection.cursor() 69 if self.connection.get_server_info() >= '4.1': 107 conn = self.threadlocal.connection = Database.connect(**kwargs) 108 else: 109 conn.ping() 110 cursor = conn.cursor() 111 if conn.get_server_info() >= '4.1': 70 112 cursor.execute("SET NAMES utf8") 71 113 if DEBUG: 72 114 return base.CursorDebugWrapper(MysqlDebugWrapper(cursor), self) 73 115 return cursor 74 116 75 117 def commit(self): 76 self.connection.commit() 118 conn = getattr(self.threadlocal, 'connection', None) 119 if conn is not None: 120 conn.commit() 77 121 78 122 def rollback(self): 79 if self.connection: 123 conn = getattr(self.threadlocal, 'connection', None) 124 if conn is not None: 80 125 try: 81 self.connection.rollback()126 conn.rollback() 82 127 except Database.NotSupportedError: 83 128 pass 84 129 85 130 def close(self): 86 if self.connection is not None: 87 self.connection.close() 88 self.connection = None 131 conn = getattr(self.threadlocal, 'connection', None) 132 if conn is not None: 133 conn.close() 134 self.threadlocal.connection = None 89 135 90 136 def quote_name(self, name): 91 137 if name.startswith("`") and name.endswith("`"):