Ticket #900: 900.2.patch

File 900.2.patch, 3.8 KB (added by Maniac <Maniac@…>, 19 years ago)

Working patch

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

     
    66
    77from django.core.db import base, typecasts
    88import psycopg as Database
     9from threading import currentThread
     10from django.utils.synch import RWLock
    911
    1012DatabaseError = Database.DatabaseError
    1113
     
    1315    def __init__(self):
    1416        self.connection = None
    1517        self.queries = []
     18        self._lock = RWLock()
     19        self._active_threads = []
    1620
    1721    def cursor(self):
    1822        from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG, TIME_ZONE
    19         if self.connection is None:
    20             if DATABASE_NAME == '':
    21                 from django.core.exceptions import ImproperlyConfigured
    22                 raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file."
    23             conn_string = "dbname=%s" % DATABASE_NAME
    24             if DATABASE_USER:
    25                 conn_string = "user=%s %s" % (DATABASE_USER, conn_string)
    26             if DATABASE_PASSWORD:
    27                 conn_string += " password='%s'" % DATABASE_PASSWORD
    28             if DATABASE_HOST:
    29                 conn_string += " host=%s" % DATABASE_HOST
    30             if DATABASE_PORT:
    31                 conn_string += " port=%s" % DATABASE_PORT
    32             self.connection = Database.connect(conn_string)
    33             self.connection.set_isolation_level(1) # make transactions transparent to all cursors
     23        self._lock.writer_enters()
     24        try:
     25            if self.connection is None:
     26                if DATABASE_NAME == '':
     27                    from django.core.exceptions import ImproperlyConfigured
     28                    raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file."
     29                conn_string = "dbname=%s" % DATABASE_NAME
     30                if DATABASE_USER:
     31                    conn_string = "user=%s %s" % (DATABASE_USER, conn_string)
     32                if DATABASE_PASSWORD:
     33                    conn_string += " password='%s'" % DATABASE_PASSWORD
     34                if DATABASE_HOST:
     35                    conn_string += " host=%s" % DATABASE_HOST
     36                if DATABASE_PORT:
     37                    conn_string += " port=%s" % DATABASE_PORT
     38                self.connection = Database.connect(conn_string)
     39                self.connection.set_isolation_level(1) # make transactions transparent to all cursors
     40            if not currentThread() in self._active_threads:
     41                self._active_threads.append(currentThread())
     42        finally:
     43            self._lock.writer_leaves()
    3444        cursor = self.connection.cursor()
    3545        cursor.execute("SET TIME ZONE %s", [TIME_ZONE])
    3646        if DEBUG:
     
    4151        return self.connection.commit()
    4252
    4353    def rollback(self):
    44         if self.connection:
    45             return self.connection.rollback()
     54        self._lock.reader_enters()
     55        try:
     56            if self.connection:
     57                return self.connection.rollback()
     58        finally:
     59            self._lock.reader_leaves()
    4660
    4761    def close(self):
    48         if self.connection is not None:
    49             self.connection.close()
    50             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()
    5171
    5272    def quote_name(self, name):
    5373        if name.startswith('"') and name.endswith('"'):
Back to Top