Ticket #900: 900.3.patch
File 900.3.patch, 3.8 KB (added by , 19 years ago) |
---|
-
django/db/backends/postgresql/base.py
6 6 7 7 from django.db.backends import util 8 8 import psycopg as Database 9 from threading import currentThread 10 from django.utils.synch import RWLock 9 11 10 12 DatabaseError = Database.DatabaseError 11 13 … … 13 15 def __init__(self): 14 16 self.connection = None 15 17 self.queries = [] 18 self._lock = RWLock() 19 self._active_threads = [] 16 20 17 21 def cursor(self): 18 22 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() 34 44 cursor = self.connection.cursor() 35 45 cursor.execute("SET TIME ZONE %s", [TIME_ZONE]) 36 46 if DEBUG: … … 41 51 return self.connection.commit() 42 52 43 53 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() 46 60 47 61 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() 51 71 52 72 def quote_name(name): 53 73 if name.startswith('"') and name.endswith('"'):