Django

Code

Ticket #3460: 3460-r8961-autocommit.diff

File 3460-r8961-autocommit.diff, 5.6 kB (added by Richard Davies <richard.davies@elastichosts.com>, 1 year ago)

Updated for Django 1.0, some problems remain (see my notes on ticket)

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

    old new  
    9696        self.validation = BaseDatabaseValidation() 
    9797 
    9898    def _cursor(self, settings): 
    99         set_tz = False 
     99        first_cursor = False 
    100100        if self.connection is None: 
    101             set_tz = True 
     101            first_cursor = True 
    102102            if settings.DATABASE_NAME == '': 
    103103                from django.core.exceptions import ImproperlyConfigured 
    104104                raise ImproperlyConfigured("You need to specify DATABASE_NAME in your Django settings file.") 
     
    112112            if settings.DATABASE_PORT: 
    113113                conn_string += " port=%s" % settings.DATABASE_PORT 
    114114            self.connection = Database.connect(conn_string, **self.options) 
    115             self.connection.set_isolation_level(1) # make transactions transparent to all cursors 
     115            self.connection.set_isolation_level(0) 
    116116        cursor = self.connection.cursor() 
    117         if set_tz
     117        if first_cursor
    118118            cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) 
     119            cursor.execute("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED") 
     120            self._commit() 
    119121            if not hasattr(self, '_version'): 
    120122                self.__class__._version = get_version(cursor) 
    121123            if self._version < (8, 0): 
  • django/db/backends/postgresql_psycopg2/base.py

    old new  
    6666        self.validation = BaseDatabaseValidation() 
    6767 
    6868    def _cursor(self, settings): 
    69         set_tz = False 
     69        first_cursor = False 
    7070        if self.connection is None: 
    71             set_tz = True 
     71            first_cursor = True 
    7272            if settings.DATABASE_NAME == '': 
    7373                from django.core.exceptions import ImproperlyConfigured 
    7474                raise ImproperlyConfigured("You need to specify DATABASE_NAME in your Django settings file.") 
     
    8282            if settings.DATABASE_PORT: 
    8383                conn_string += " port=%s" % settings.DATABASE_PORT 
    8484            self.connection = Database.connect(conn_string, **self.options) 
    85             self.connection.set_isolation_level(1) # make transactions transparent to all cursors 
     85            self.connection.set_isolation_level(0) 
    8686            self.connection.set_client_encoding('UTF8') 
    8787        cursor = self.connection.cursor() 
    8888        cursor.tzinfo_factory = None 
    89         if set_tz
     89        if first_cursor
    9090            cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) 
     91            cursor.execute("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED") 
     92            self._commit() 
    9193            if not hasattr(self, '_version'): 
    9294                self.__class__._version = get_version(cursor) 
    9395            if self._version < (8, 0): 
  • django/db/transaction.py

    old new  
    185185        savepoint_state[thread_ident] = [None] 
    186186    tid = str(thread_ident).replace('-', '') 
    187187    sid = "s%s_x%d" % (tid, len(savepoint_state[thread_ident])) 
    188     connection._savepoint(sid) 
     188    if is_managed(): 
     189        connection._savepoint(sid) 
    189190    return sid 
    190191 
    191192def savepoint_rollback(sid): 
     
    194195    savepoints are not supported. 
    195196    """ 
    196197    if thread.get_ident() in savepoint_state: 
    197         connection._savepoint_rollback(sid) 
     198        if is_managed(): 
     199            connection._savepoint_rollback(sid) 
    198200 
    199201def savepoint_commit(sid): 
    200202    """ 
     
    202204    savepoints are not supported. 
    203205    """ 
    204206    if thread.get_ident() in savepoint_state: 
    205         connection._savepoint_commit(sid) 
     207        if is_managed(): 
     208            connection._savepoint_commit(sid) 
    206209 
    207210############## 
    208211# DECORATORS # 
  • django/core/management/commands/loaddata.py

    old new  
    99except NameError: 
    1010    from sets import Set as set   # Python 2.3 fallback 
    1111 
     12def _set_transaction_mode(connection): 
     13    "Make sure a connection is not in autocommit mode."  
     14    if hasattr(connection.connection, "autocommit"):  
     15        connection.connection.autocommit(False)  
     16    elif hasattr(connection.connection, "set_isolation_level"):  
     17        connection.connection.set_isolation_level(1)  
     18 
    1219class Command(BaseCommand): 
    1320    option_list = BaseCommand.option_list + ( 
    1421        make_option('--verbosity', action='store', dest='verbosity', default='1', 
     
    4956        # the side effect of initializing the test database (if 
    5057        # it isn't already initialized). 
    5158        cursor = connection.cursor() 
     59        _set_transaction_mode(connection) 
    5260 
    5361        # Start transaction management. All fixtures are installed in a 
    5462        # single transaction to ensure that all references are resolved. 
     
    177185        # edge case in MySQL: if the same connection is used to 
    178186        # create tables, load data, and query, the query can return 
    179187        # incorrect results. See Django #7572, MySQL #37735. 
     188        # Also used to ensure _set_transaction_mode does not leak out 
    180189        if commit: 
    181190            connection.close()