Removed _enter/_leave_transaction_management.
The goal is to make all databases share a common, autocommit-based, implementation.
This commit is contained in:
parent
1617557ae3
commit
cfc114e00e
|
@ -231,21 +231,6 @@ class BaseDatabaseWrapper(object):
|
||||||
|
|
||||||
##### Backend-specific transaction management methods #####
|
##### 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):
|
def _set_autocommit(self, autocommit):
|
||||||
"""
|
"""
|
||||||
Backend-specific implementation to enable or disable 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.
|
commit/rollback, the data will be commited, unless "forced" is True.
|
||||||
"""
|
"""
|
||||||
self.transaction_state.append(managed)
|
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:
|
if not managed and self.is_dirty() and not forced:
|
||||||
self.commit()
|
self.commit()
|
||||||
|
|
||||||
|
@ -283,17 +271,21 @@ class BaseDatabaseWrapper(object):
|
||||||
else:
|
else:
|
||||||
raise TransactionManagementError(
|
raise TransactionManagementError(
|
||||||
"This code isn't under transaction management")
|
"This code isn't under transaction management")
|
||||||
# The _leave_transaction_management hook can change the dirty flag,
|
|
||||||
# so memoize it.
|
# That's the next state -- we already left the previous state behind.
|
||||||
dirty = self._dirty
|
managed = self.is_managed()
|
||||||
# We will pass the next status (after leaving the previous state
|
|
||||||
# behind) to subclass hook.
|
if self._dirty:
|
||||||
self._leave_transaction_management(self.is_managed())
|
|
||||||
if dirty:
|
|
||||||
self.rollback()
|
self.rollback()
|
||||||
|
if not managed and not self.autocommit:
|
||||||
|
self.set_autocommit(True)
|
||||||
raise TransactionManagementError(
|
raise TransactionManagementError(
|
||||||
"Transaction managed block ended with pending COMMIT/ROLLBACK")
|
"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):
|
def set_autocommit(self, autocommit=True):
|
||||||
"""
|
"""
|
||||||
Enable or disable autocommit.
|
Enable or disable autocommit.
|
||||||
|
|
|
@ -55,8 +55,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
_savepoint = ignore
|
_savepoint = ignore
|
||||||
_savepoint_commit = complain
|
_savepoint_commit = complain
|
||||||
_savepoint_rollback = ignore
|
_savepoint_rollback = ignore
|
||||||
_enter_transaction_management = complain
|
|
||||||
_leave_transaction_management = ignore
|
|
||||||
_set_autocommit = complain
|
_set_autocommit = complain
|
||||||
set_dirty = complain
|
set_dirty = complain
|
||||||
set_clean = complain
|
set_clean = complain
|
||||||
|
|
|
@ -164,23 +164,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
finally:
|
finally:
|
||||||
self.set_clean()
|
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):
|
def _set_isolation_level(self, isolation_level):
|
||||||
assert isolation_level in range(1, 5) # Use set_autocommit for level = 0
|
assert isolation_level in range(1, 5) # Use set_autocommit for level = 0
|
||||||
if self.psycopg2_version >= (2, 4, 2):
|
if self.psycopg2_version >= (2, 4, 2):
|
||||||
|
|
Loading…
Reference in New Issue