Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made transaction.managed a no-op and deprecated it.

enter_transaction_management() was nearly always followed by managed().

In three places it wasn't, but they will all be refactored eventually.
The "forced" keyword argument avoids introducing behavior changes until
then.

This is mostly backwards-compatible, except, of course, for managed
itself. There's a minor difference in _enter_transaction_management:
the top self.transaction_state now contains the new 'managed' state
rather than the previous one. Django doesn't access
self.transaction_state in _enter_transaction_management.
  • Loading branch information...
commit 7aacde84f2b499d9c35741cbfccb621af6b48903 1 parent 9cec689
Aymeric Augustin authored March 02, 2013
1  django/core/management/commands/loaddata.py
@@ -75,7 +75,6 @@ def handle(self, *fixture_labels, **options):
75 75
         if commit:
76 76
             transaction.commit_unless_managed(using=self.using)
77 77
             transaction.enter_transaction_management(using=self.using)
78  
-            transaction.managed(True, using=self.using)
79 78
 
80 79
         class SingleZipReader(zipfile.ZipFile):
81 80
             def __init__(self, *args, **kwargs):
29  django/db/backends/__init__.py
@@ -234,7 +234,7 @@ def _leave_transaction_management(self, managed):
234 234
 
235 235
     ##### Generic transaction management methods #####
236 236
 
237  
-    def enter_transaction_management(self, managed=True):
  237
+    def enter_transaction_management(self, managed=True, forced=False):
238 238
         """
239 239
         Enters transaction management for a running thread. It must be balanced with
240 240
         the appropriate leave_transaction_management call, since the actual state is
@@ -243,12 +243,14 @@ def enter_transaction_management(self, managed=True):
243 243
         The state and dirty flag are carried over from the surrounding block or
244 244
         from the settings, if there is no surrounding block (dirty is always false
245 245
         when no current block is running).
  246
+
  247
+        If you switch off transaction management and there is a pending
  248
+        commit/rollback, the data will be commited, unless "forced" is True.
246 249
         """
247  
-        if self.transaction_state:
248  
-            self.transaction_state.append(self.transaction_state[-1])
249  
-        else:
250  
-            self.transaction_state.append(settings.TRANSACTIONS_MANAGED)
  250
+        self.transaction_state.append(managed)
251 251
         self._enter_transaction_management(managed)
  252
+        if not managed and self.is_dirty() and not forced:
  253
+            self.commit()
252 254
 
253 255
     def leave_transaction_management(self):
254 256
         """
@@ -314,22 +316,6 @@ def is_managed(self):
314 316
             return self.transaction_state[-1]
315 317
         return settings.TRANSACTIONS_MANAGED
316 318
 
317  
-    def managed(self, flag=True):
318  
-        """
319  
-        Puts the transaction manager into a manual state: managed transactions have
320  
-        to be committed explicitly by the user. If you switch off transaction
321  
-        management and there is a pending commit/rollback, the data will be
322  
-        commited.
323  
-        """
324  
-        top = self.transaction_state
325  
-        if top:
326  
-            top[-1] = flag
327  
-            if not flag and self.is_dirty():
328  
-                self.commit()
329  
-        else:
330  
-            raise TransactionManagementError("This code isn't under transaction "
331  
-                "management")
332  
-
333 319
     def commit_unless_managed(self):
334 320
         """
335 321
         Commits changes if the system is not in managed transaction mode.
@@ -574,7 +560,6 @@ def supports_transactions(self):
574 560
             # otherwise autocommit will cause the confimation to
575 561
             # fail.
576 562
             self.connection.enter_transaction_management()
577  
-            self.connection.managed(True)
578 563
             cursor = self.connection.cursor()
579 564
             cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
580 565
             self.connection.commit()
2  django/db/models/deletion.py
@@ -54,7 +54,7 @@ def force_managed(func):
54 54
     @wraps(func)
55 55
     def decorated(self, *args, **kwargs):
56 56
         if not transaction.is_managed(using=self.using):
57  
-            transaction.enter_transaction_management(using=self.using)
  57
+            transaction.enter_transaction_management(using=self.using, forced=True)
58 58
             forced_managed = True
59 59
         else:
60 60
             forced_managed = False
4  django/db/models/query.py
@@ -443,7 +443,7 @@ def bulk_create(self, objs, batch_size=None):
443 443
         connection = connections[self.db]
444 444
         fields = self.model._meta.local_fields
445 445
         if not transaction.is_managed(using=self.db):
446  
-            transaction.enter_transaction_management(using=self.db)
  446
+            transaction.enter_transaction_management(using=self.db, forced=True)
447 447
             forced_managed = True
448 448
         else:
449 449
             forced_managed = False
@@ -582,7 +582,7 @@ def update(self, **kwargs):
582 582
         query = self.query.clone(sql.UpdateQuery)
583 583
         query.add_update_values(kwargs)
584 584
         if not transaction.is_managed(using=self.db):
585  
-            transaction.enter_transaction_management(using=self.db)
  585
+            transaction.enter_transaction_management(using=self.db, forced=True)
586 586
             forced_managed = True
587 587
         else:
588 588
             forced_managed = False
18  django/db/transaction.py
@@ -12,6 +12,8 @@
12 12
 or implicit commits or rollbacks.
13 13
 """
14 14
 
  15
+import warnings
  16
+
15 17
 from functools import wraps
16 18
 
17 19
 from django.db import connections, DEFAULT_DB_ALIAS
@@ -49,7 +51,7 @@ def abort(using=None):
49 51
     """
50 52
     get_connection(using).abort()
51 53
 
52  
-def enter_transaction_management(managed=True, using=None):
  54
+def enter_transaction_management(managed=True, using=None, forced=False):
53 55
     """
54 56
     Enters transaction management for a running thread. It must be balanced with
55 57
     the appropriate leave_transaction_management call, since the actual state is
@@ -59,7 +61,7 @@ def enter_transaction_management(managed=True, using=None):
59 61
     from the settings, if there is no surrounding block (dirty is always false
60 62
     when no current block is running).
61 63
     """
62  
-    get_connection(using).enter_transaction_management(managed)
  64
+    get_connection(using).enter_transaction_management(managed, forced)
63 65
 
64 66
 def leave_transaction_management(using=None):
65 67
     """
@@ -105,13 +107,8 @@ def is_managed(using=None):
105 107
     return get_connection(using).is_managed()
106 108
 
107 109
 def managed(flag=True, using=None):
108  
-    """
109  
-    Puts the transaction manager into a manual state: managed transactions have
110  
-    to be committed explicitly by the user. If you switch off transaction
111  
-    management and there is a pending commit/rollback, the data will be
112  
-    commited.
113  
-    """
114  
-    get_connection(using).managed(flag)
  110
+    warnings.warn("'managed' no longer serves a purpose.",
  111
+        PendingDeprecationWarning, stacklevel=2)
115 112
 
116 113
 def commit_unless_managed(using=None):
117 114
     """
@@ -224,7 +221,6 @@ def autocommit(using=None):
224 221
     """
225 222
     def entering(using):
226 223
         enter_transaction_management(managed=False, using=using)
227  
-        managed(False, using=using)
228 224
 
229 225
     def exiting(exc_value, using):
230 226
         leave_transaction_management(using=using)
@@ -240,7 +236,6 @@ def commit_on_success(using=None):
240 236
     """
241 237
     def entering(using):
242 238
         enter_transaction_management(using=using)
243  
-        managed(True, using=using)
244 239
 
245 240
     def exiting(exc_value, using):
246 241
         try:
@@ -268,7 +263,6 @@ def commit_manually(using=None):
268 263
     """
269 264
     def entering(using):
270 265
         enter_transaction_management(using=using)
271  
-        managed(True, using=using)
272 266
 
273 267
     def exiting(exc_value, using):
274 268
         leave_transaction_management(using=using)
1  django/middleware/transaction.py
@@ -10,7 +10,6 @@ class TransactionMiddleware(object):
10 10
     def process_request(self, request):
11 11
         """Enters transaction management"""
12 12
         transaction.enter_transaction_management()
13  
-        transaction.managed(True)
14 13
 
15 14
     def process_exception(self, request, exception):
16 15
         """Rolls back the database and leaves transaction management"""
4  django/test/testcases.py
@@ -67,7 +67,6 @@ def to_list(value):
67 67
 real_rollback = transaction.rollback
68 68
 real_enter_transaction_management = transaction.enter_transaction_management
69 69
 real_leave_transaction_management = transaction.leave_transaction_management
70  
-real_managed = transaction.managed
71 70
 real_abort = transaction.abort
72 71
 
73 72
 def nop(*args, **kwargs):
@@ -78,7 +77,6 @@ def disable_transaction_methods():
78 77
     transaction.rollback = nop
79 78
     transaction.enter_transaction_management = nop
80 79
     transaction.leave_transaction_management = nop
81  
-    transaction.managed = nop
82 80
     transaction.abort = nop
83 81
 
84 82
 def restore_transaction_methods():
@@ -86,7 +84,6 @@ def restore_transaction_methods():
86 84
     transaction.rollback = real_rollback
87 85
     transaction.enter_transaction_management = real_enter_transaction_management
88 86
     transaction.leave_transaction_management = real_leave_transaction_management
89  
-    transaction.managed = real_managed
90 87
     transaction.abort = real_abort
91 88
 
92 89
 
@@ -833,7 +830,6 @@ def _fixture_setup(self):
833 830
 
834 831
         for db_name in self._databases_names():
835 832
             transaction.enter_transaction_management(using=db_name)
836  
-            transaction.managed(True, using=db_name)
837 833
         disable_transaction_methods()
838 834
 
839 835
         from django.contrib.sites.models import Site
6  docs/internals/deprecation.txt
@@ -339,8 +339,6 @@ these changes.
339 339
 
340 340
 * ``Model._meta.module_name`` was renamed to ``model_name``.
341 341
 
342  
-* The private API ``django.db.close_connection`` will be removed.
343  
-
344 342
 * Remove the backward compatible shims introduced to rename ``get_query_set``
345 343
   and similar queryset methods. This affects the following classes:
346 344
   ``BaseModelAdmin``, ``ChangeList``, ``BaseCommentNode``,
@@ -350,6 +348,10 @@ these changes.
350 348
 * Remove the backward compatible shims introduced to rename the attributes
351 349
   ``ChangeList.root_query_set`` and ``ChangeList.query_set``.
352 350
 
  351
+* The private API ``django.db.close_connection`` will be removed.
  352
+
  353
+* The private API ``django.transaction.managed`` will be removed.
  354
+
353 355
 2.0
354 356
 ---
355 357
 
4  tests/delete_regress/tests.py
@@ -22,9 +22,7 @@ def setUp(self):
22 22
         self.conn2 = new_connections[DEFAULT_DB_ALIAS]
23 23
         # Put both DB connections into managed transaction mode
24 24
         transaction.enter_transaction_management()
25  
-        transaction.managed(True)
26 25
         self.conn2.enter_transaction_management()
27  
-        self.conn2.managed(True)
28 26
 
29 27
     def tearDown(self):
30 28
         # Close down the second connection.
@@ -335,7 +333,7 @@ def test_ticket_19102_select_related(self):
335 333
             ).select_related('orgunit').delete()
336 334
         self.assertFalse(Login.objects.filter(pk=self.l1.pk).exists())
337 335
         self.assertTrue(Login.objects.filter(pk=self.l2.pk).exists())
338  
-    
  336
+
339 337
     @skipUnlessDBFeature("update_can_self_select")
340 338
     def test_ticket_19102_defer(self):
341 339
         with self.assertNumQueries(1):
6  tests/middleware/tests.py
@@ -692,7 +692,6 @@ def test_request(self):
692 692
 
693 693
     def test_managed_response(self):
694 694
         transaction.enter_transaction_management()
695  
-        transaction.managed(True)
696 695
         Band.objects.create(name='The Beatles')
697 696
         self.assertTrue(transaction.is_dirty())
698 697
         TransactionMiddleware().process_response(self.request, self.response)
@@ -700,8 +699,7 @@ def test_managed_response(self):
700 699
         self.assertEqual(Band.objects.count(), 1)
701 700
 
702 701
     def test_unmanaged_response(self):
703  
-        transaction.enter_transaction_management()
704  
-        transaction.managed(False)
  702
+        transaction.enter_transaction_management(False)
705 703
         self.assertEqual(Band.objects.count(), 0)
706 704
         TransactionMiddleware().process_response(self.request, self.response)
707 705
         self.assertFalse(transaction.is_managed())
@@ -711,7 +709,6 @@ def test_unmanaged_response(self):
711 709
 
712 710
     def test_exception(self):
713 711
         transaction.enter_transaction_management()
714  
-        transaction.managed(True)
715 712
         Band.objects.create(name='The Beatles')
716 713
         self.assertTrue(transaction.is_dirty())
717 714
         TransactionMiddleware().process_exception(self.request, None)
@@ -726,7 +723,6 @@ def raise_exception():
726 723
                 raise IntegrityError()
727 724
             connections[DEFAULT_DB_ALIAS].commit = raise_exception
728 725
             transaction.enter_transaction_management()
729  
-            transaction.managed(True)
730 726
             Band.objects.create(name='The Beatles')
731 727
             self.assertTrue(transaction.is_dirty())
732 728
             with self.assertRaises(IntegrityError):
2  tests/requests/tests.py
@@ -576,7 +576,6 @@ def test_request_finished_db_state(self):
576 576
         # Make sure there is an open connection
577 577
         connection.cursor()
578 578
         connection.enter_transaction_management()
579  
-        connection.managed(True)
580 579
         signals.request_finished.send(sender=response._handler_class)
581 580
         self.assertEqual(len(connection.transaction_state), 0)
582 581
 
@@ -585,7 +584,6 @@ def test_request_finished_failed_connection(self):
585 584
         connection.settings_dict['CONN_MAX_AGE'] = 0
586 585
 
587 586
         connection.enter_transaction_management()
588  
-        connection.managed(True)
589 587
         connection.set_dirty()
590 588
         # Test that the rollback doesn't succeed (for example network failure
591 589
         # could cause this).
3  tests/select_for_update/tests.py
@@ -25,7 +25,6 @@ class SelectForUpdateTests(TransactionTestCase):
25 25
 
26 26
     def setUp(self):
27 27
         transaction.enter_transaction_management()
28  
-        transaction.managed(True)
29 28
         self.person = Person.objects.create(name='Reinhardt')
30 29
 
31 30
         # We have to commit here so that code in run_select_for_update can
@@ -37,7 +36,6 @@ def setUp(self):
37 36
         new_connections = ConnectionHandler(settings.DATABASES)
38 37
         self.new_connection = new_connections[DEFAULT_DB_ALIAS]
39 38
         self.new_connection.enter_transaction_management()
40  
-        self.new_connection.managed(True)
41 39
 
42 40
         # We need to set settings.DEBUG to True so we can capture
43 41
         # the output SQL to examine.
@@ -162,7 +160,6 @@ def run_select_for_update(self, status, nowait=False):
162 160
             # We need to enter transaction management again, as this is done on
163 161
             # per-thread basis
164 162
             transaction.enter_transaction_management()
165  
-            transaction.managed(True)
166 163
             people = list(
167 164
                 Person.objects.all().select_for_update(nowait=nowait)
168 165
             )
1  tests/serializers/tests.py
@@ -268,7 +268,6 @@ def test_forward_refs(self):
268 268
         # within a transaction in order to test forward reference
269 269
         # handling.
270 270
         transaction.enter_transaction_management()
271  
-        transaction.managed(True)
272 271
         objs = serializers.deserialize(self.serializer_name, self.fwd_ref_str)
273 272
         with connection.constraint_checks_disabled():
274 273
             for obj in objs:
5  tests/transactions_regress/tests.py
@@ -223,7 +223,6 @@ def test_commit_unless_managed(self):
223 223
     def test_commit_unless_managed_in_managed(self):
224 224
         cursor = connection.cursor()
225 225
         connection.enter_transaction_management()
226  
-        transaction.managed(True)
227 226
         cursor.execute("INSERT into transactions_regress_mod (fld) values (2)")
228 227
         connection.commit_unless_managed()
229 228
         self.assertTrue(connection.is_dirty())
@@ -280,7 +279,6 @@ def test_initial_autocommit_state(self):
280 279
 
281 280
     def test_transaction_management(self):
282 281
         transaction.enter_transaction_management()
283  
-        transaction.managed(True)
284 282
         self.assertEqual(connection.isolation_level, self._serializable)
285 283
 
286 284
         transaction.leave_transaction_management()
@@ -288,7 +286,6 @@ def test_transaction_management(self):
288 286
 
289 287
     def test_transaction_stacking(self):
290 288
         transaction.enter_transaction_management()
291  
-        transaction.managed(True)
292 289
         self.assertEqual(connection.isolation_level, self._serializable)
293 290
 
294 291
         transaction.enter_transaction_management()
@@ -302,13 +299,11 @@ def test_transaction_stacking(self):
302 299
 
303 300
     def test_enter_autocommit(self):
304 301
         transaction.enter_transaction_management()
305  
-        transaction.managed(True)
306 302
         self.assertEqual(connection.isolation_level, self._serializable)
307 303
         list(Mod.objects.all())
308 304
         self.assertTrue(transaction.is_dirty())
309 305
         # Enter autocommit mode again.
310 306
         transaction.enter_transaction_management(False)
311  
-        transaction.managed(False)
312 307
         self.assertFalse(transaction.is_dirty())
313 308
         self.assertEqual(
314 309
             connection.connection.get_transaction_status(),

0 notes on commit 7aacde8

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