Skip to content

Commit

Permalink
Deprecated transaction.is_managed().
Browse files Browse the repository at this point in the history
It's synchronized with the autocommit flag.
  • Loading branch information
aaugustin committed Mar 11, 2013
1 parent ba5138b commit 3bdc7a6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 30 deletions.
30 changes: 12 additions & 18 deletions django/db/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,12 @@ def enter_transaction_management(self, managed=True, forced=False):
"""
self.transaction_state.append(managed)

if managed and self.autocommit:
self.set_autocommit(False)

if not managed and self.is_dirty() and not forced:
self.commit()
self.set_clean()

if managed == self.autocommit:
self.set_autocommit(not managed)

def leave_transaction_management(self):
"""
Expand All @@ -274,19 +275,20 @@ def leave_transaction_management(self):
raise TransactionManagementError(
"This code isn't under transaction management")

# That's the next state -- we already left the previous state behind.
managed = self.is_managed()
if self.transaction_state:
managed = self.transaction_state[-1]
else:
managed = settings.TRANSACTIONS_MANAGED

if self._dirty:
self.rollback()
if not managed and not self.autocommit:
self.set_autocommit(True)
if managed == self.autocommit:
self.set_autocommit(not managed)
raise TransactionManagementError(
"Transaction managed block ended with pending COMMIT/ROLLBACK")

if not managed and not self.autocommit:
self.set_autocommit(True)

if managed == self.autocommit:
self.set_autocommit(not managed)

def set_autocommit(self, autocommit=True):
"""
Expand Down Expand Up @@ -331,14 +333,6 @@ def set_clean(self):
self._dirty = False
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 #####

@contextmanager
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def DO_NOTHING(collector, field, sub_objs, using):
def force_managed(func):
@wraps(func)
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)
forced_managed = True
else:
Expand Down
4 changes: 2 additions & 2 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def bulk_create(self, objs, batch_size=None):
self._for_write = True
connection = connections[self.db]
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)
forced_managed = True
else:
Expand Down Expand Up @@ -579,7 +579,7 @@ def update(self, **kwargs):
self._for_write = True
query = self.query.clone(sql.UpdateQuery)
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)
forced_managed = True
else:
Expand Down
12 changes: 5 additions & 7 deletions django/db/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ def clean_savepoints(using=None):
get_connection(using).clean_savepoints()

def is_managed(using=None):
"""
Checks whether the transaction manager is in manual or in auto state.
"""
return get_connection(using).is_managed()
warnings.warn("'is_managed' is deprecated.",
PendingDeprecationWarning, stacklevel=2)

def managed(flag=True, using=None):
warnings.warn("'managed' no longer serves a purpose.",
Expand Down Expand Up @@ -281,13 +279,13 @@ def commit_on_success_unless_managed(using=None):
"""
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):
pass

def exiting(exc_value, using):
set_dirty(using=using)

return _transaction_func(entering, exiting, using)
else:
return commit_on_success(using)
2 changes: 1 addition & 1 deletion django/middleware/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def process_exception(self, request, exception):

def process_response(self, request, response):
"""Commits and leaves transaction management."""
if transaction.is_managed():
if not transaction.get_autocommit():
if transaction.is_dirty():
# Note: it is possible that the commit fails. If the reason is
# closed connection or some similar reason, then there is
Expand Down
1 change: 1 addition & 0 deletions docs/internals/deprecation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ these changes.
* The following private APIs will be removed:
- ``django.db.close_connection()``
- ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()``
- ``django.db.transaction.is_managed()``
- ``django.db.transaction.managed()``
- ``django.db.transaction.commit_unless_managed()``
- ``django.db.transaction.rollback_unless_managed()``
Expand Down
2 changes: 1 addition & 1 deletion tests/middleware/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def tearDown(self):

def test_request(self):
TransactionMiddleware().process_request(self.request)
self.assertTrue(transaction.is_managed())
self.assertFalse(transaction.get_autocommit())

def test_managed_response(self):
transaction.enter_transaction_management()
Expand Down

0 comments on commit 3bdc7a6

Please sign in to comment.