Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #11900 -- Corrected an edge case of transaction handlin…

…g in the commit_on_success decorator. Thanks to guettli for the report, and Gabriel Hurley for the initial test case.

Backport of r12764 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12765 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1e1b57bfbe3a493d2179d499f00b0bbba49b8e2d 1 parent 9137c54
Russell Keith-Magee authored March 12, 2010
6  django/db/transaction.py
@@ -245,7 +245,11 @@ def _commit_on_success(*args, **kw):
245 245
                 raise
246 246
             else:
247 247
                 if is_dirty():
248  
-                    commit()
  248
+                    try:
  249
+                        commit()
  250
+                    except:
  251
+                        rollback()
  252
+                        raise
249 253
             return res
250 254
         finally:
251 255
             leave_transaction_management()
20  tests/modeltests/transactions/models.py
@@ -100,3 +100,23 @@ def __unicode__(self):
100 100
     ...
101 101
 TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
102 102
 """
  103
+
  104
+# Regression for #11900: If a function wrapped by commit_on_success writes a
  105
+# transaction that can't be committed, that transaction should be rolled back.
  106
+# The bug is only visible using the psycopg2 backend, though
  107
+# the fix is generally a good idea.
  108
+if building_docs or settings.DATABASE_ENGINE == 'postgresql_psycopg2':
  109
+    __test__['API_TESTS'] += """
  110
+>>> def execute_bad_sql():
  111
+...     cursor = connection.cursor()
  112
+...     cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');")
  113
+...     transaction.set_dirty()
  114
+...
  115
+>>> execute_bad_sql = transaction.commit_on_success(execute_bad_sql)
  116
+>>> execute_bad_sql()
  117
+Traceback (most recent call last):
  118
+    ...
  119
+IntegrityError: null value in column "email" violates not-null constraint
  120
+<BLANKLINE>
  121
+
  122
+"""

0 notes on commit 1e1b57b

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