Ticket #463: mysql.patch

File mysql.patch, 3.8 KB (added by eugene@…, 10 years ago)

patch as a file

  • mysql.py

     
    1111from MySQLdb.constants import FIELD_TYPE
    1212import types
    1313
     14import thread, threading
     15from sets import Set
     16
    1417DatabaseError = Database.DatabaseError
    1518
    1619django_conversions = conversions.copy()
     
    2326
    2427class DatabaseWrapper:
    2528    def __init__(self):
    26         self.connection = None
     29        self.connections = {}
     30        self.threads = Set()
     31        self.lock = thread.allocate_lock()
    2732        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()
    2862
    2963    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
    3466        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()
    3769
    3870    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()
    4078
    4179    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()
    4790
    4891    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()
    52101
    53102def get_last_insert_id(cursor, table_name, pk_name):
    54103    cursor.execute("SELECT LAST_INSERT_ID()")
Back to Top