Ticket #463: mysql.patch
File mysql.patch, 3.8 KB (added by , 19 years ago) |
---|
-
mysql.py
11 11 from MySQLdb.constants import FIELD_TYPE 12 12 import types 13 13 14 import thread, threading 15 from sets import Set 16 14 17 DatabaseError = Database.DatabaseError 15 18 16 19 django_conversions = conversions.copy() … … 23 26 24 27 class DatabaseWrapper: 25 28 def __init__(self): 26 self.connection = None 29 self.connections = {} 30 self.threads = Set() 31 self.lock = thread.allocate_lock() 27 32 self.queries = [] 33 34 def _get_connection(self): 35 self.lock.acquire() 36 try: 37 # find existing connection 38 id = threading.currentThread().getName() 39 if id in self.connections: 40 connection = self.connections[id] 41 connection.ping() 42 return connection 43 # normalize thread name 44 if id != 'MainThread': 45 id = str(thread.get_ident()) 46 threading.currentThread().setName(id) 47 # remove deadwood 48 dead = self.threads - Set([x.getName() for x in threading.enumerate()]) 49 for name in dead: 50 self.connections[name].close() 51 del self.connections[name] 52 self.threads -= dead 53 # create new connection 54 from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PASSWORD 55 connection = Database.connect(user=DATABASE_USER, db=DATABASE_NAME, 56 passwd=DATABASE_PASSWORD, host=DATABASE_HOST, conv=django_conversions) 57 self.connections[id] = connection 58 self.threads.add(id) 59 return connection 60 finally: 61 self.lock.release() 28 62 29 63 def cursor(self): 30 from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PASSWORD, DEBUG 31 if self.connection is None: 32 self.connection = Database.connect(user=DATABASE_USER, db=DATABASE_NAME, 33 passwd=DATABASE_PASSWORD, host=DATABASE_HOST, conv=django_conversions) 64 connection = self._get_connection() 65 from django.conf.settings import DEBUG 34 66 if DEBUG: 35 return base.CursorDebugWrapper( self.connection.cursor(), self)36 return self.connection.cursor()67 return base.CursorDebugWrapper(connection.cursor(), self) 68 return connection.cursor() 37 69 38 70 def commit(self): 39 self.connection.commit() 71 self.lock.acquire() 72 try: 73 id = threading.currentThread().getName() 74 if id in self.connections: 75 self.connections[id].commit() 76 finally: 77 self.lock.release() 40 78 41 79 def rollback(self): 42 if self.connection: 43 try: 44 self.connection.rollback() 45 except Database.NotSupportedError: 46 pass 80 self.lock.acquire() 81 try: 82 id = threading.currentThread().getName() 83 if id in self.connections: 84 try: 85 self.connections[id].rollback() 86 except Database.NotSupportedError: 87 pass 88 finally: 89 self.lock.release() 47 90 48 91 def close(self): 49 if self.connection is not None: 50 self.connection.close() 51 self.connection = None 92 self.lock.acquire() 93 try: 94 id = threading.currentThread().getName() 95 if id in self.connections: 96 connection = self.connections[id] 97 connection.close() 98 del self.connections[id] 99 finally: 100 self.lock.release() 52 101 53 102 def get_last_insert_id(cursor, table_name, pk_name): 54 103 cursor.execute("SELECT LAST_INSERT_ID()")