From 549c495875d279c17fbd418238a305bcc51cdad6 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 19 Nov 2011 14:18:44 +0000 Subject: [PATCH] Used symbolic constants for psycopg2 isolation levels. Django used the value 1 = ISOLATION_LEVEL_READ_UNCOMMITTED in some places, but PostgreSQL doesn't provide "read uncommited", it uses "read committed" instead: http://www.postgresql.org/docs/9.1/static/transaction-iso.html. For clarity, this commit uses ISOLATION_LEVEL_READ_COMMITTED = 2 where 1 was previously used. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17112 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/postgresql_psycopg2/base.py | 12 ++++++++---- django/db/backends/postgresql_psycopg2/creation.py | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 87df6bd1bc..dbd49c55e1 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -108,7 +108,11 @@ class DatabaseWrapper(BaseDatabaseWrapper): self.features = DatabaseFeatures(self) autocommit = self.settings_dict["OPTIONS"].get('autocommit', False) self.features.uses_autocommit = autocommit - self._set_isolation_level(int(not autocommit)) + if autocommit: + level = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT + else: + level = psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED + self._set_isolation_level(level) self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) @@ -189,7 +193,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._set_isolation_level(1) + self._set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED) def _leave_transaction_management(self, managed): """ @@ -197,7 +201,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): leaving transaction management. """ if self.features.uses_autocommit and not managed and self.isolation_level: - self._set_isolation_level(0) + self._set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) def _set_isolation_level(self, level): """ @@ -205,7 +209,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): levels. This doesn't touch the uses_autocommit feature, since that controls the movement *between* isolation levels. """ - assert level in (0, 1) + assert level in range(5) try: if self.connection is not None: self.connection.set_isolation_level(level) diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py index e4e7428ea7..ca389b9046 100644 --- a/django/db/backends/postgresql_psycopg2/creation.py +++ b/django/db/backends/postgresql_psycopg2/creation.py @@ -1,3 +1,5 @@ +import psycopg2.extensions + from django.db.backends.creation import BaseDatabaseCreation from django.db.backends.util import truncate_name @@ -81,4 +83,5 @@ class DatabaseCreation(BaseDatabaseCreation): def _prepare_for_test_db_ddl(self): """Rollback and close the active transaction.""" self.connection.connection.rollback() - self.connection.connection.set_isolation_level(0) + self.connection.connection.set_isolation_level( + psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)