Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Deprecated transaction.is_managed().

It's synchronized with the autocommit flag.
  • Loading branch information...
commit 3bdc7a6a70bb030324fdebe9b1dce1fa5358f0c6 1 parent ba5138b
Aymeric Augustin authored March 04, 2013
30  django/db/backends/__init__.py
@@ -256,11 +256,12 @@ def enter_transaction_management(self, managed=True, forced=False):
256 256
         """
257 257
         self.transaction_state.append(managed)
258 258
 
259  
-        if managed and self.autocommit:
260  
-            self.set_autocommit(False)
261  
-
262 259
         if not managed and self.is_dirty() and not forced:
263 260
             self.commit()
  261
+            self.set_clean()
  262
+
  263
+        if managed == self.autocommit:
  264
+            self.set_autocommit(not managed)
264 265
 
265 266
     def leave_transaction_management(self):
266 267
         """
@@ -274,19 +275,20 @@ def leave_transaction_management(self):
274 275
             raise TransactionManagementError(
275 276
                 "This code isn't under transaction management")
276 277
 
277  
-        # That's the next state -- we already left the previous state behind.
278  
-        managed = self.is_managed()
  278
+        if self.transaction_state:
  279
+            managed = self.transaction_state[-1]
  280
+        else:
  281
+            managed = settings.TRANSACTIONS_MANAGED
279 282
 
280 283
         if self._dirty:
281 284
             self.rollback()
282  
-            if not managed and not self.autocommit:
283  
-                self.set_autocommit(True)
  285
+            if managed == self.autocommit:
  286
+                self.set_autocommit(not managed)
284 287
             raise TransactionManagementError(
285 288
                 "Transaction managed block ended with pending COMMIT/ROLLBACK")
286 289
 
287  
-        if not managed and not self.autocommit:
288  
-            self.set_autocommit(True)
289  
-
  290
+        if managed == self.autocommit:
  291
+            self.set_autocommit(not managed)
290 292
 
291 293
     def set_autocommit(self, autocommit=True):
292 294
         """
@@ -331,14 +333,6 @@ def set_clean(self):
331 333
         self._dirty = False
332 334
         self.clean_savepoints()
333 335
 
334  
-    def is_managed(self):
335  
-        """
336  
-        Checks whether the transaction manager is in manual or in auto state.
337  
-        """
338  
-        if self.transaction_state:
339  
-            return self.transaction_state[-1]
340  
-        return settings.TRANSACTIONS_MANAGED
341  
-
342 336
     ##### Foreign key constraints checks handling #####
343 337
 
344 338
     @contextmanager
2  django/db/models/deletion.py
@@ -53,7 +53,7 @@ def DO_NOTHING(collector, field, sub_objs, using):
53 53
 def force_managed(func):
54 54
     @wraps(func)
55 55
     def decorated(self, *args, **kwargs):
56  
-        if not transaction.is_managed(using=self.using):
  56
+        if transaction.get_autocommit(using=self.using):
57 57
             transaction.enter_transaction_management(using=self.using, forced=True)
58 58
             forced_managed = True
59 59
         else:
4  django/db/models/query.py
@@ -442,7 +442,7 @@ def bulk_create(self, objs, batch_size=None):
442 442
         self._for_write = True
443 443
         connection = connections[self.db]
444 444
         fields = self.model._meta.local_fields
445  
-        if not transaction.is_managed(using=self.db):
  445
+        if transaction.get_autocommit(using=self.db):
446 446
             transaction.enter_transaction_management(using=self.db, forced=True)
447 447
             forced_managed = True
448 448
         else:
@@ -579,7 +579,7 @@ def update(self, **kwargs):
579 579
         self._for_write = True
580 580
         query = self.query.clone(sql.UpdateQuery)
581 581
         query.add_update_values(kwargs)
582  
-        if not transaction.is_managed(using=self.db):
  582
+        if transaction.get_autocommit(using=self.db):
583 583
             transaction.enter_transaction_management(using=self.db, forced=True)
584 584
             forced_managed = True
585 585
         else:
12  django/db/transaction.py
@@ -113,10 +113,8 @@ def clean_savepoints(using=None):
113 113
     get_connection(using).clean_savepoints()
114 114
 
115 115
 def is_managed(using=None):
116  
-    """
117  
-    Checks whether the transaction manager is in manual or in auto state.
118  
-    """
119  
-    return get_connection(using).is_managed()
  116
+    warnings.warn("'is_managed' is deprecated.",
  117
+        PendingDeprecationWarning, stacklevel=2)
120 118
 
121 119
 def managed(flag=True, using=None):
122 120
     warnings.warn("'managed' no longer serves a purpose.",
@@ -281,7 +279,9 @@ def commit_on_success_unless_managed(using=None):
281 279
     """
282 280
     Transitory API to preserve backwards-compatibility while refactoring.
283 281
     """
284  
-    if is_managed(using):
  282
+    if get_autocommit(using):
  283
+        return commit_on_success(using)
  284
+    else:
285 285
         def entering(using):
286 286
             pass
287 287
 
@@ -289,5 +289,3 @@ def exiting(exc_value, using):
289 289
             set_dirty(using=using)
290 290
 
291 291
         return _transaction_func(entering, exiting, using)
292  
-    else:
293  
-        return commit_on_success(using)
2  django/middleware/transaction.py
@@ -23,7 +23,7 @@ def process_exception(self, request, exception):
23 23
 
24 24
     def process_response(self, request, response):
25 25
         """Commits and leaves transaction management."""
26  
-        if transaction.is_managed():
  26
+        if not transaction.get_autocommit():
27 27
             if transaction.is_dirty():
28 28
                 # Note: it is possible that the commit fails. If the reason is
29 29
                 # closed connection or some similar reason, then there is
1  docs/internals/deprecation.txt
@@ -351,6 +351,7 @@ these changes.
351 351
 * The following private APIs will be removed:
352 352
   - ``django.db.close_connection()``
353 353
   - ``django.db.backends.creation.BaseDatabaseCreation.set_autocommit()``
  354
+  - ``django.db.transaction.is_managed()``
354 355
   - ``django.db.transaction.managed()``
355 356
   - ``django.db.transaction.commit_unless_managed()``
356 357
   - ``django.db.transaction.rollback_unless_managed()``
2  tests/middleware/tests.py
@@ -689,7 +689,7 @@ def tearDown(self):
689 689
 
690 690
     def test_request(self):
691 691
         TransactionMiddleware().process_request(self.request)
692  
-        self.assertTrue(transaction.is_managed())
  692
+        self.assertFalse(transaction.get_autocommit())
693 693
 
694 694
     def test_managed_response(self):
695 695
         transaction.enter_transaction_management()

0 notes on commit 3bdc7a6

Please sign in to comment.
Something went wrong with that request. Please try again.