diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index eee52587914..5972df3cb4d 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -63,12 +63,9 @@ class DatabaseWrapper(BaseDatabaseWrapper): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures() - if settings.DATABASE_OPTIONS.get('autocommit', False): - self.features.uses_autocommit = True - self._iso_level_0() - else: - self.features.uses_autocommit = False - self._iso_level_1() + autocommit = self.settings_dict["DATABASE_OPTIONS"].get('autocommit', False) + self.features.uses_autocommit = autocommit + self._set_isolation_level(int(not autocommit)) self.ops = DatabaseOperations() self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) @@ -116,7 +113,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): the same transaction is visible across all the queries. """ if self.features.uses_autocommit and managed and not self.isolation_level: - self._iso_level_1() + self._set_isolation_level(1) def _leave_transaction_management(self, managed): """ @@ -124,29 +121,19 @@ class DatabaseWrapper(BaseDatabaseWrapper): leaving transaction management. """ if self.features.uses_autocommit and not managed and self.isolation_level: - self._iso_level_0() + self._set_isolation_level(0) - def _iso_level_0(self): + def _set_isolation_level(self, level): """ - Do all the related feature configurations for isolation level 0. This - doesn't touch the uses_autocommit feature, since that controls the - movement *between* isolation levels. + Do all the related feature configurations for changing isolation + levels. This doesn't touch the uses_autocommit feature, since that + controls the movement *between* isolation levels. """ + assert level in (0, 1) try: if self.connection is not None: - self.connection.set_isolation_level(0) + self.connection.set_isolation_level(level) finally: - self.isolation_level = 0 - self.features.uses_savepoints = False - - def _iso_level_1(self): - """ - The "isolation level 1" version of _iso_level_0(). - """ - try: - if self.connection is not None: - self.connection.set_isolation_level(1) - finally: - self.isolation_level = 1 - self.features.uses_savepoints = True + self.isolation_level = level + self.features.uses_savepoints = bool(level)