Removed _enter/_leave_transaction_management.

The goal is to make all databases share a common, autocommit-based,
implementation.
This commit is contained in:
Aymeric Augustin 2013-03-03 15:48:52 +01:00
parent 1617557ae3
commit cfc114e00e
3 changed files with 15 additions and 42 deletions

View File

@ -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.

View File

@ -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

View File

@ -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):