Ticket #463: mysql_rev2307.diff
File mysql_rev2307.diff, 4.1 KB (added by , 19 years ago) |
---|
-
mysql.py
10 10 from MySQLdb.converters import conversions 11 11 from MySQLdb.constants import FIELD_TYPE 12 12 import types 13 import threading 14 from sets import Set 15 from django.utils.synch import RWLock 13 16 17 14 18 DatabaseError = Database.DatabaseError 15 19 16 20 django_conversions = conversions.copy() … … 49 53 50 54 class DatabaseWrapper: 51 55 def __init__(self): 52 self.connection = None 56 self.__connections = {} 57 self.__threads = Set() 58 self.__lock = RWLock() 53 59 self.queries = [] 54 55 def cursor(self): 56 from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG 57 if self.connection is None: 60 def _valid_connection(self, con): 61 try: 62 con.ping() 63 return True 64 except DatabaseError: 65 con.close() # close connection 66 return False 67 68 def __connect(self): 69 id = threading.currentThread() 70 self.__lock.reader_enters() 71 try: 72 if (id in self.__connections) and (self._valid_connection(self.__connections[id])): 73 return self.__connections[id] 74 finally: 75 self.__lock.reader_leaves() 76 77 self.__lock.writer_enters() 78 try: 79 # remove deadwood 80 dead = self.__threads - Set(threading.enumerate()) 81 for name in dead: 82 self.__connections[name].close() 83 del self.__connections[name] 84 self.__threads -= dead 85 86 # create new connection 87 from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD 58 88 kwargs = { 59 89 'user': DATABASE_USER, 60 90 'db': DATABASE_NAME, … … 64 94 } 65 95 if DATABASE_PORT: 66 96 kwargs['port'] = DATABASE_PORT 67 self.connection = Database.connect(**kwargs) 68 cursor = self.connection.cursor() 69 if self.connection.get_server_info() >= '4.1': 97 connection = Database.connect(**kwargs) 98 self.__connections[id] = connection 99 self.__threads.add(id) 100 return connection 101 finally: 102 self.__lock.writer_leaves() 103 104 def cursor(self): 105 from django.conf.settings import DEBUG 106 connection = self.__connect() 107 cursor = connection.cursor() 108 if connection.get_server_info() >= '4.1': 70 109 cursor.execute("SET NAMES utf8") 71 110 if DEBUG: 72 111 return base.CursorDebugWrapper(MysqlDebugWrapper(cursor), self) 73 112 return cursor 74 113 75 114 def commit(self): 76 self.connection.commit() 115 id = threading.currentThread() 116 self.__lock.reader_enters() 117 try: 118 if id in self.__connections: 119 self.__connections[id].commit() 120 finally: 121 self.__lock.reader_leaves() 77 122 78 123 def rollback(self): 79 if self.connection: 80 try: 81 self.connection.rollback() 82 except Database.NotSupportedError: 83 pass 124 id = threading.currentThread() 125 self.__lock.reader_enters() 126 try: 127 if id in self.__connections: 128 try: 129 self.__connections[id].rollback() 130 except Database.NotSupportedError: 131 pass 132 finally: 133 self.__lock.reader_leaves() 84 134 85 135 def close(self): 86 if self.connection is not None: 87 self.connection.close() 88 self.connection = None 136 id = threading.currentThread() 137 self.__lock.writer_enters() 138 try: 139 if id in self.__connections: 140 self.__connections[id].close() 141 del self.__connections[id] 142 finally: 143 self.__lock.writer_leaves() 89 144 90 145 def quote_name(self, name): 91 146 if name.startswith("`") and name.endswith("`"):