Deprecated transaction.is_managed().

It's synchronized with the autocommit flag.
This commit is contained in:
Aymeric Augustin 2013-03-04 15:24:01 +01:00
parent ba5138b1c0
commit 3bdc7a6a70
7 changed files with 23 additions and 30 deletions

View File

@ -256,11 +256,12 @@ class BaseDatabaseWrapper(object):
""" """
self.transaction_state.append(managed) self.transaction_state.append(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()
self.set_clean()
if managed == self.autocommit:
self.set_autocommit(not managed)
def leave_transaction_management(self): def leave_transaction_management(self):
""" """
@ -274,19 +275,20 @@ class BaseDatabaseWrapper(object):
raise TransactionManagementError( raise TransactionManagementError(
"This code isn't under transaction management") "This code isn't under transaction management")
# That's the next state -- we already left the previous state behind. if self.transaction_state:
managed = self.is_managed() managed = self.transaction_state[-1]
else:
managed = settings.TRANSACTIONS_MANAGED
if self._dirty: if self._dirty:
self.rollback() self.rollback()
if not managed and not self.autocommit: if managed == self.autocommit:
self.set_autocommit(True) self.set_autocommit(not managed)
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: if managed == self.autocommit:
self.set_autocommit(True) self.set_autocommit(not managed)
def set_autocommit(self, autocommit=True): def set_autocommit(self, autocommit=True):
""" """
@ -331,14 +333,6 @@ class BaseDatabaseWrapper(object):
self._dirty = False self._dirty = False
self.clean_savepoints() self.clean_savepoints()
def is_managed(self):
"""
Checks whether the transaction manager is in manual or in auto state.
"""
if self.transaction_state:
return self.transaction_state[-1]
return settings.TRANSACTIONS_MANAGED
##### Foreign key constraints checks handling ##### ##### Foreign key constraints checks handling #####
@contextmanager @contextmanager

View File

@ -53,7 +53,7 @@ def DO_NOTHING(collector, field, sub_objs, using):
def force_managed(func): def force_managed(func):
@wraps(func) @wraps(func)
def decorated(self, *args, **kwargs): def decorated(self, *args, **kwargs):
if not transaction.is_managed(using=self.using): if transaction.get_autocommit(using=self.using):
transaction.enter_transaction_management(using=self.using, forced=True) transaction.enter_transaction_management(using=self.using, forced=True)
forced_managed = True forced_managed = True
else: else:

View File

@ -442,7 +442,7 @@ class QuerySet(object):
self._for_write = True self._for_write = True
connection = connections[self.db] connection = connections[self.db]
fields = self.model._meta.local_fields fields = self.model._meta.local_fields
if not transaction.is_managed(using=self.db): if transaction.get_autocommit(using=self.db):
transaction.enter_transaction_management(using=self.db, forced=True) transaction.enter_transaction_management(using=self.db, forced=True)
forced_managed = True forced_managed = True
else: else:
@ -579,7 +579,7 @@ class QuerySet(object):
self._for_write = True self._for_write = True
query = self.query.clone(sql.UpdateQuery) query = self.query.clone(sql.UpdateQuery)
query.add_update_values(kwargs) query.add_update_values(kwargs)
if not transaction.is_managed(using=self.db): if transaction.get_autocommit(using=self.db):
transaction.enter_transaction_management(using=self.db, forced=True) transaction.enter_transaction_management(using=self.db, forced=True)
forced_managed = True forced_managed = True
else: else:

View File

@ -113,10 +113,8 @@ def clean_savepoints(using=None):
get_connection(using).clean_savepoints() get_connection(using).clean_savepoints()
def is_managed(using=None): def is_managed(using=None):
""" warnings.warn("'is_managed' is deprecated.",
Checks whether the transaction manager is in manual or in auto state. PendingDeprecationWarning, stacklevel=2)
"""
return get_connection(using).is_managed()
def managed(flag=True, using=None): def managed(flag=True, using=None):
warnings.warn("'managed' no longer serves a purpose.", warnings.warn("'managed' no longer serves a purpose.",
@ -281,7 +279,9 @@ def commit_on_success_unless_managed(using=None):
""" """
Transitory API to preserve backwards-compatibility while refactoring. Transitory API to preserve backwards-compatibility while refactoring.
""" """
if is_managed(using): if get_autocommit(using):
return commit_on_success(using)
else:
def entering(using): def entering(using):
pass pass
@ -289,5 +289,3 @@ def commit_on_success_unless_managed(using=None):
set_dirty(using=using) set_dirty(using=using)
return _transaction_func(entering, exiting, using) return _transaction_func(entering, exiting, using)
else:
return commit_on_success(using)

View File

@ -23,7 +23,7 @@ class TransactionMiddleware(object):
def process_response(self, request, response): def process_response(self, request, response):
"""Commits and leaves transaction management.""" """Commits and leaves transaction management."""
if transaction.is_managed(): if not transaction.get_autocommit():
if transaction.is_dirty(): if transaction.is_dirty():
# Note: it is possible that the commit fails. If the reason is # Note: it is possible that the commit fails. If the reason is
# closed connection or some similar reason, then there is # closed connection or some similar reason, then there is

View File

@ -351,6 +351,7 @@ these changes.
* The following private APIs will be removed: * The following private APIs will be removed:
- ``django.db.close_connection()`` - ``django.db.close_connection()``
- ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()`` - ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()``
- ``django.db.transaction.is_managed()``
- ``django.db.transaction.managed()`` - ``django.db.transaction.managed()``
- ``django.db.transaction.commit_unless_managed()`` - ``django.db.transaction.commit_unless_managed()``
- ``django.db.transaction.rollback_unless_managed()`` - ``django.db.transaction.rollback_unless_managed()``

View File

@ -689,7 +689,7 @@ class TransactionMiddlewareTest(TransactionTestCase):
def test_request(self): def test_request(self):
TransactionMiddleware().process_request(self.request) TransactionMiddleware().process_request(self.request)
self.assertTrue(transaction.is_managed()) self.assertFalse(transaction.get_autocommit())
def test_managed_response(self): def test_managed_response(self):
transaction.enter_transaction_management() transaction.enter_transaction_management()