From d4677d4bfbf73110a436037199b6938ce5ea167f Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 12 Mar 2009 05:30:51 +0000 Subject: [PATCH] Slight refactoring of isolation setting from r10029. There was a bug in the way we were reading the DATABASE_OPTIONS setting and a lot of essentially duplicated code. This is neater. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10033 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../db/backends/postgresql_psycopg2/base.py | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index eee5258791..5972df3cb4 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)