From cfc114e00ebe2ac16c37af2ccee1ed8e47247b7a Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 3 Mar 2013 15:48:52 +0100 Subject: [PATCH] Removed _enter/_leave_transaction_management. The goal is to make all databases share a common, autocommit-based, implementation. --- django/db/backends/__init__.py | 38 ++++++++----------- django/db/backends/dummy/base.py | 2 - .../db/backends/postgresql_psycopg2/base.py | 17 --------- 3 files changed, 15 insertions(+), 42 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index b7f4628e9a..26a84e6e60 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -231,21 +231,6 @@ class BaseDatabaseWrapper(object): ##### Backend-specific transaction management methods ##### - def _enter_transaction_management(self, managed): - """ - A hook for backend-specific changes required when entering manual - transaction handling. - """ - pass - - def _leave_transaction_management(self, managed): - """ - A hook for backend-specific changes required when leaving manual - transaction handling. Will usually be implemented only when - _enter_transaction_management() is also required. - """ - pass - def _set_autocommit(self, autocommit): """ Backend-specific implementation to enable or disable autocommit. @@ -268,7 +253,10 @@ class BaseDatabaseWrapper(object): commit/rollback, the data will be commited, unless "forced" is True. """ self.transaction_state.append(managed) - self._enter_transaction_management(managed) + + if managed and self.autocommit: + self.set_autocommit(False) + if not managed and self.is_dirty() and not forced: self.commit() @@ -283,17 +271,21 @@ class BaseDatabaseWrapper(object): else: raise TransactionManagementError( "This code isn't under transaction management") - # The _leave_transaction_management hook can change the dirty flag, - # so memoize it. - dirty = self._dirty - # We will pass the next status (after leaving the previous state - # behind) to subclass hook. - self._leave_transaction_management(self.is_managed()) - if dirty: + + # That's the next state -- we already left the previous state behind. + managed = self.is_managed() + + if self._dirty: self.rollback() + if not managed and not self.autocommit: + self.set_autocommit(True) raise TransactionManagementError( "Transaction managed block ended with pending COMMIT/ROLLBACK") + if not managed and not self.autocommit: + self.set_autocommit(True) + + def set_autocommit(self, autocommit=True): """ Enable or disable autocommit. diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index a8c2d5bada..02f0b6462d 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -55,8 +55,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): _savepoint = ignore _savepoint_commit = complain _savepoint_rollback = ignore - _enter_transaction_management = complain - _leave_transaction_management = ignore _set_autocommit = complain set_dirty = complain set_clean = complain diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 384b38cc58..6ea2dd3099 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -164,23 +164,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): finally: self.set_clean() - def _enter_transaction_management(self, managed): - """ - Switch the isolation level when needing transaction support, so that - the same transaction is visible across all the queries. - """ - if managed and self.autocommit: - self.set_autocommit(False) - - def _leave_transaction_management(self, managed): - """ - If the normal operating mode is "autocommit", switch back to that when - leaving transaction management. - """ - if not managed and not self.autocommit: - self.rollback() # Must terminate transaction first. - self.set_autocommit(True) - def _set_isolation_level(self, isolation_level): assert isolation_level in range(1, 5) # Use set_autocommit for level = 0 if self.psycopg2_version >= (2, 4, 2):