Author: russellm
Date: 2010-03-12 08:10:01 -0600 (Fri, 12 Mar 2010)
New Revision: 12764

Modified:
   django/trunk/django/db/transaction.py
   django/trunk/tests/modeltests/transactions/models.py
Log:
Fixed #11900 -- Corrected an edge case of transaction handling in the 
commit_on_success decorator. Thanks to guettli for the report, and Gabriel 
Hurley for the initial test case.

Modified: django/trunk/django/db/transaction.py
===================================================================
--- django/trunk/django/db/transaction.py       2010-03-12 13:31:10 UTC (rev 
12763)
+++ django/trunk/django/db/transaction.py       2010-03-12 14:10:01 UTC (rev 
12764)
@@ -304,7 +304,11 @@
                     raise
                 else:
                     if is_dirty(using=db):
-                        commit(using=db)
+                        try:
+                            commit(using=db)
+                        except:
+                            rollback(using=db)
+                            raise
                 return res
             finally:
                 leave_transaction_management(using=db)

Modified: django/trunk/tests/modeltests/transactions/models.py
===================================================================
--- django/trunk/tests/modeltests/transactions/models.py        2010-03-12 
13:31:10 UTC (rev 12763)
+++ django/trunk/tests/modeltests/transactions/models.py        2010-03-12 
14:10:01 UTC (rev 12764)
@@ -130,3 +130,24 @@
 TransactionManagementError: Transaction managed block ended with pending 
COMMIT/ROLLBACK
 
 """
+
+# Regression for #11900: If a function wrapped by commit_on_success writes a
+# transaction that can't be committed, that transaction should be rolled back.
+# The bug is only visible using the psycopg2 backend, though
+# the fix is generally a good idea.
+pgsql_backends = ('django.db.backends.postgresql_psycopg2', 
'postgresql_psycopg2',)
+if building_docs or settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] in 
pgsql_backends:
+    __test__['API_TESTS'] += """
+>>> def execute_bad_sql():
+...     cursor = connection.cursor()
+...     cursor.execute("INSERT INTO transactions_reporter (first_name, 
last_name) VALUES ('Douglas', 'Adams');")
+...     transaction.set_dirty()
+...
+>>> execute_bad_sql = transaction.commit_on_success(execute_bad_sql)
+>>> execute_bad_sql()
+Traceback (most recent call last):
+    ...
+IntegrityError: null value in column "email" violates not-null constraint
+<BLANKLINE>
+
+"""

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to