Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented atomic_if_autocommit.

It disables transaction management entirely when AUTOCOMMIT is False.
  • Loading branch information...
commit f7245b83bb2df9d66a375d46a4be8de093957fa7 1 parent 09ba70f
Aymeric Augustin authored March 07, 2013

Showing 1 changed file with 22 additions and 4 deletions. Show diff stats Hide diff stats

  1. 26  django/db/transaction.py
26  django/db/transaction.py
@@ -308,6 +308,24 @@ def atomic(using=None):
308 308
         return Atomic(using)
309 309
 
310 310
 
  311
+def atomic_if_autocommit(using=None):
  312
+    # This variant only exists to support the ability to disable transaction
  313
+    # management entirely in the DATABASES setting. It doesn't care about the
  314
+    # autocommit state at run time.
  315
+    db = DEFAULT_DB_ALIAS if callable(using) else using
  316
+    autocommit = get_connection(db).settings_dict['AUTOCOMMIT']
  317
+
  318
+    if autocommit:
  319
+        return atomic(using)
  320
+    else:
  321
+        # Bare decorator: @atomic_if_autocommit
  322
+        if callable(using):
  323
+            return using
  324
+        # Decorator: @atomic_if_autocommit(...)
  325
+        else:
  326
+            return lambda func: func
  327
+
  328
+
311 329
 ############################################
312 330
 # Deprecated decorators / context managers #
313 331
 ############################################
@@ -431,13 +449,13 @@ def commit_on_success_unless_managed(using=None):
431 449
     Transitory API to preserve backwards-compatibility while refactoring.
432 450
 
433 451
     Once the legacy transaction management is fully deprecated, this should
434  
-    simply be replaced by atomic. Until then, it's necessary to avoid making a
435  
-    commit where Django didn't use to, since entering atomic in managed mode
436  
-    triggers a commmit.
  452
+    simply be replaced by atomic_if_autocommit. Until then, it's necessary to
  453
+    avoid making a commit where Django didn't use to, since entering atomic in
  454
+    managed mode triggers a commmit.
437 455
     """
438 456
     connection = get_connection(using)
439 457
     if connection.autocommit or connection.in_atomic_block:
440  
-        return atomic(using)
  458
+        return atomic_if_autocommit(using)
441 459
     else:
442 460
         def entering(using):
443 461
             pass

0 notes on commit f7245b8

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