Ticket #463: mysql_magic-removal_rev2360.diff

File mysql_magic-removal_rev2360.diff, 2.3 KB (added by greg@…, 19 years ago)

Patch for magic-removal 2360, using thread-local storage

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

     
    99from MySQLdb.converters import conversions
    1010from MySQLdb.constants import FIELD_TYPE
    1111import types
     12import threading
    1213
    1314DatabaseError = Database.DatabaseError
    1415
     
    4849
    4950class DatabaseWrapper:
    5051    def __init__(self):
    51         self.connection = None
     52        self.threadlocal = threading.local()
    5253        self.queries = []
    5354
    5455    def cursor(self):
    5556        from django.conf import settings
    56         if self.connection is None:
     57        conn = getattr(self.threadlocal, 'connection', None)
     58        if conn is None:
    5759            kwargs = {
    5860                'user': settings.DATABASE_USER,
    5961                'db': settings.DATABASE_NAME,
     
    6365            }
    6466            if settings.DATABASE_PORT:
    6567                kwargs['port'] = settings.DATABASE_PORT
    66             self.connection = Database.connect(**kwargs)
    67         cursor = self.connection.cursor()
    68         if self.connection.get_server_info() >= '4.1':
     68            conn = self.threadlocal.connection = Database.connect(**kwargs)
     69        else:
     70            conn.ping()
     71        cursor = conn.cursor()
     72        if conn.get_server_info() >= '4.1':
    6973            cursor.execute("SET NAMES utf8")
    7074        if settings.DEBUG:
    7175            return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
    7276        return cursor
    73 
     77   
    7478    def commit(self):
    75         self.connection.commit()
     79        conn = getattr(self.threadlocal, 'connection', None)
     80        if conn is not None:
     81            conn.commit()
    7682
    7783    def rollback(self):
    78         if self.connection:
     84        conn = getattr(self.threadlocal, 'connection', None)
     85        if conn is not None:
    7986            try:
    80                 self.connection.rollback()
     87                conn.rollback()
    8188            except Database.NotSupportedError:
    8289                pass
    8390
    8491    def close(self):
    85         if self.connection is not None:
    86             self.connection.close()
    87             self.connection = None
     92        conn = getattr(self.threadlocal, 'connection', None)
     93        if conn is not None:
     94            conn.close()
     95            self.threadlocal.connection = None
    8896
    8997supports_constraints = True
    9098
Back to Top