From fbe1abac4af3a7fc138bd176471e36acb1070a58 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 12 Jan 2014 20:27:08 +0100 Subject: [PATCH] Fixed #21453 -- Enabled autocommit before calling init_connection_state. Also ensured the transaction state is clean on Oracle while I was there. This change cannot be backported to 1.6 because it's backwards-incompatible for custom database backends. --- django/db/backends/__init__.py | 3 +-- django/db/backends/oracle/base.py | 3 +++ django/db/backends/postgresql_psycopg2/base.py | 10 +++++----- docs/releases/1.7.txt | 5 +++++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 00ae64e09b3..2dbb8b3aaec 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -110,9 +110,8 @@ class BaseDatabaseWrapper(object): # Establish the connection conn_params = self.get_connection_params() self.connection = self.get_new_connection(conn_params) + self.set_autocommit(self.settings_dict['AUTOCOMMIT']) self.init_connection_state() - if self.settings_dict['AUTOCOMMIT']: - self.set_autocommit(True) connection_created.send(sender=self.__class__, connection=self) def ensure_connection(self): diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index aae3240e120..cdb101d20ca 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -627,6 +627,9 @@ class DatabaseWrapper(BaseDatabaseWrapper): # Django docs specify cx_Oracle version 4.3.1 or higher, but # stmtcachesize is available only in 4.3.2 and up. pass + # Ensure all changes are preserved even when AUTOCOMMIT is False. + if not self.get_autocommit(): + self.commit() def create_cursor(self): return FormatStylePlaceholderCursor(self.connection) diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index c7fd98ae98e..7725b0c7a09 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -142,12 +142,12 @@ class DatabaseWrapper(BaseDatabaseWrapper): conn_tz = get_parameter_status('TimeZone') if conn_tz != tz: - self.connection.cursor().execute( - self.ops.set_time_zone_sql(), [tz] - ) + cursor = self.connection.cursor() + cursor.execute(self.ops.set_time_zone_sql(), [tz]) + cursor.close() # Commit after setting the time zone (see #17062) - self.connection.commit() - self.connection.set_isolation_level(self.isolation_level) + if not self.get_autocommit(): + self.connection.commit() def create_cursor(self): cursor = self.connection.cursor() diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index fdbf63526e7..671b5878b9b 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -899,6 +899,11 @@ Miscellaneous * GeoDjango dropped support for GEOS < 3.1. +* The ``init_connection_state`` method of database backends now executes in + autocommit mode (unless you set :setting:`AUTOCOMMIT ` + to ``False``). If you maintain a custom database backend, you should check + that method. + Features deprecated in 1.7 ==========================