diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2015-02-14 09:50:38 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2015-02-14 18:51:11 +0100 |
| commit | 76356d963c131252e1ce4285083fd21fd0bdedd9 (patch) | |
| tree | b34a5b2b5e2f83ca7d2257973b706a67178abb86 /django/db/backends/postgresql_psycopg2 | |
| parent | 1379165b3548b843980e59201eea8961a0b30a2d (diff) | |
Fixed #24318 -- Set the transaction isolation level with psycopg >= 2.4.2.
Diffstat (limited to 'django/db/backends/postgresql_psycopg2')
| -rw-r--r-- | django/db/backends/postgresql_psycopg2/base.py | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 4410a173a0..d2e114fea5 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -127,10 +127,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) - opts = self.settings_dict["OPTIONS"] - RC = psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED - self.isolation_level = opts.get('isolation_level', RC) - self.features = DatabaseFeatures(self) self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) @@ -165,7 +161,29 @@ class DatabaseWrapper(BaseDatabaseWrapper): return conn_params def get_new_connection(self, conn_params): - return Database.connect(**conn_params) + connection = Database.connect(**conn_params) + + # self.isolation_level must be set: + # - after connecting to the database in order to obtain the database's + # default when no value is explicitly specified in options. + # - before calling _set_autocommit() because if autocommit is on, that + # will set connection.isolation_level to ISOLATION_LEVEL_AUTOCOMMIT; + # and if autocommit is off, on psycopg2 < 2.4.2, _set_autocommit() + # needs self.isolation_level. + options = self.settings_dict['OPTIONS'] + try: + self.isolation_level = options['isolation_level'] + except KeyError: + self.isolation_level = connection.isolation_level + else: + # Set the isolation level to the value from OPTIONS. This isn't + # needed on psycopg2 < 2.4.2 because it happens as a side-effect + # of _set_autocommit(False). + if (self.isolation_level != connection.isolation_level and + self.psycopg2_version >= (2, 4, 2)): + connection.set_session(isolation_level=self.isolation_level) + + return connection def init_connection_state(self): settings_dict = self.settings_dict |
