Ticket #1533: sqlitebase_02.patch

File sqlitebase_02.patch, 2.7 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 or not currentThread() in self._active_threads:
     37                connection = Database.connect(settings.DATABASE_NAME, detect_types=Database.PARSE_DECLTYPES)
     38                # register extract and date_trun functions
     39                connection.create_function("django_extract", 2, _sqlite_extract)
     40                connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
     41                self._active_threads[currentThread()] = connection
     42            self.connection = self._active_threads[currentThread()]
     43        finally:
     44            self._lock.writer_leaves()
    3545        cursor = self.connection.cursor(factory=SQLiteCursorWrapper)
    3646        cursor.row_factory = utf8rowFactory
    3747        if settings.DEBUG:
     
    4353        self.connection.commit()
    4454
    4555    def _rollback(self):
    46         if self.connection:
    47             self.connection.rollback()
    48 
     56        self._lock.reader_enters()
     57        try:
     58            if self.connection:
     59                return self.connection.rollback()
     60        finally:
     61            self._lock.reader_leaves()
     62           
    4963    def close(self):
    50         if self.connection is not None:
    51             self.connection.close()
    52             self.connection = None
     64        self._lock.writer_enters()
     65        try:
     66            if currentThread() in self._active_threads:
     67                self._active_threads.pop(currentThread())
     68            if not self._active_threads and self.connection is not None:
     69                self.connection.close()
     70                self.connection = None
     71        finally:
     72            self._lock.writer_leaves()
    5373
    5474class SQLiteCursorWrapper(Database.Cursor):
    5575    """
Back to Top