Ticket #1533: sqlitebase.patch

File sqlitebase.patch, 2.6 KB (added by limodou@…, 19 years ago)
  • base.py

     
    44
    55from django.db.backends import util
    66from pysqlite2 import dbapi2 as Database
    7 
     7from threading import currentThread 
     8from django.utils.synch import RWLock
     9   
    810DatabaseError = Database.DatabaseError
    911
    1012Database.register_converter("bool", lambda s: str(s) == '1')
     
    2426    def __init__(self):
    2527        self.connection = None
    2628        self.queries = []
    27 
     29        self._lock = RWLock()
     30        self._active_threads = []
     31   
    2832    def cursor(self):
    2933        from django.conf import settings
    30         if self.connection is None:
    31             self.connection = Database.connect(settings.DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES)
    32             # register extract and date_trun functions
    33             self.connection.create_function("django_extract", 2, _sqlite_extract)
    34             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
     34        self._lock.writer_enters()
     35        try:
     36            if self.connection is None:
     37                self.connection = Database.connect(settings.DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES)
     38                # register extract and date_trun functions
     39                self.connection.create_function("django_extract", 2, _sqlite_extract)
     40                self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
     41        finally:
     42            self._lock.writer_leaves()
    3543        cursor = self.connection.cursor(factory=SQLiteCursorWrapper)
    3644        cursor.row_factory = utf8rowFactory
    3745        if settings.DEBUG:
     
    4351        self.connection.commit()
    4452
    4553    def _rollback(self):
    46         if self.connection:
    47             self.connection.rollback()
    48 
     54        self._lock.reader_enters()
     55        try:
     56            if self.connection:
     57                return self.connection.rollback()
     58        finally:
     59            self._lock.reader_leaves()
     60           
    4961    def close(self):
    50         if self.connection is not None:
    51             self.connection.close()
    52             self.connection = None
     62        self._lock.writer_enters()
     63        try:
     64            if currentThread() in self._active_threads:
     65                self._active_threads.remove(currentThread())
     66            if not self._active_threads and self.connection is not None:
     67                self.connection.close()
     68                self.connection = None
     69        finally:
     70            self._lock.writer_leaves()
    5371
    5472class SQLiteCursorWrapper(Database.Cursor):
    5573    """
Back to Top