Django

Code

Ticket #3460: 3460-autocommit.diff

File 3460-autocommit.diff, 2.8 kB (added by Timothée Peignier <tim@tryphon.org>, 6 months ago)

Change isolation level without breaking fixtures and tests

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

    old new  
    5151    } 
    5252 
    5353    def _cursor(self, settings): 
    54         set_tz = False 
     54        first_cursor = False 
    5555        if self.connection is None: 
    56             set_tz = True 
     56            first_cursor = True 
    5757            if settings.DATABASE_NAME == '': 
    5858                from django.core.exceptions import ImproperlyConfigured 
    5959                raise ImproperlyConfigured("You need to specify DATABASE_NAME in your Django settings file.") 
     
    6767            if settings.DATABASE_PORT: 
    6868                conn_string += " port=%s" % settings.DATABASE_PORT 
    6969            self.connection = Database.connect(conn_string, **self.options) 
    70             self.connection.set_isolation_level(1) # make transactions transparent to all cursors 
     70            self.connection.set_isolation_level(0) # make transactions transparent to all cursors 
    7171            self.connection.set_client_encoding('UTF8') 
    7272        cursor = self.connection.cursor() 
    7373        cursor.tzinfo_factory = None 
    74         if set_tz
     74        if first_cursor
    7575            cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE]) 
     76            cursor.execute("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED") 
     77            self._commit() 
    7678        return cursor 
  • 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', 
     
    4047        # the side effect of initializing the test database (if 
    4148        # it isn't already initialized). 
    4249        cursor = connection.cursor() 
     50        _set_transaction_mode(connection) 
    4351 
    4452        # Start transaction management. All fixtures are installed in a 
    4553        # single transaction to ensure that all references are resolved. 
     
    129137 
    130138        transaction.commit() 
    131139        transaction.leave_transaction_management() 
     140        connection.close() 
    132141 
    133142        if object_count == 0: 
    134143            if verbosity >= 2: