Ticket #463: mysql_trunk_rev2360.diff

File mysql_trunk_rev2360.diff, 2.4 KB (added by greg@…, 19 years ago)

Patch for trunk 2360, using thread-local storage

  • django/core/db/backends/mysql.py

     
    1010from MySQLdb.converters import conversions
    1111from MySQLdb.constants import FIELD_TYPE
    1212import types
     13import threading
    1314
    1415DatabaseError = Database.DatabaseError
    1516
     
    4950
    5051class DatabaseWrapper:
    5152    def __init__(self):
    52         self.connection = None
     53        self.threadlocal = threading.local()
    5354        self.queries = []
    5455
    5556    def cursor(self):
    5657        from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG
    57         if self.connection is None:
     58        conn = getattr(self.threadlocal, 'connection', None)
     59        if conn is None:
    5860            kwargs = {
    5961                'user': DATABASE_USER,
    6062                'db': DATABASE_NAME,
     
    6466            }
    6567            if DATABASE_PORT:
    6668                kwargs['port'] = DATABASE_PORT
    67             self.connection = Database.connect(**kwargs)
    68         cursor = self.connection.cursor()
    69         if self.connection.get_server_info() >= '4.1':
     69            conn = self.threadlocal.connection = Database.connect(**kwargs)
     70        else:
     71            conn.ping()
     72        cursor = conn.cursor()
     73        if conn.get_server_info() >= '4.1':
    7074            cursor.execute("SET NAMES utf8")
    7175        if DEBUG:
    7276            return base.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
    7377        return cursor
    7478
    7579    def commit(self):
    76         self.connection.commit()
     80        conn = getattr(self.threadlocal, 'connection', None)
     81        if conn is not None:
     82            conn.commit()
    7783
    7884    def rollback(self):
    79         if self.connection:
     85        conn = getattr(self.threadlocal, 'connection', None)
     86        if conn is not None:
    8087            try:
    81                 self.connection.rollback()
     88                conn.rollback()
    8289            except Database.NotSupportedError:
    8390                pass
    8491
    8592    def close(self):
    86         if self.connection is not None:
    87             self.connection.close()
    88             self.connection = None
     93        conn = getattr(self.threadlocal, 'connection', None)
     94        if conn is not None:
     95            conn.close()
     96            self.threadlocal.connection = None
    8997
    9098    def quote_name(self, name):
    9199        if name.startswith("`") and name.endswith("`"):
Back to Top