Skip to content

Commit

Permalink
[1.1.X] Fixed #11900 -- Corrected an edge case of transaction handlin…
Browse files Browse the repository at this point in the history
…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
freakboy3742 committed Mar 12, 2010
1 parent 9137c54 commit 1e1b57b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django/db/transaction.py
Expand Up @@ -245,7 +245,11 @@ def _commit_on_success(*args, **kw):
raise raise
else: else:
if is_dirty(): if is_dirty():
commit() try:
commit()
except:
rollback()
raise
return res return res
finally: finally:
leave_transaction_management() leave_transaction_management()
Expand Down
20 changes: 20 additions & 0 deletions tests/modeltests/transactions/models.py
Expand Up @@ -100,3 +100,23 @@ def __unicode__(self):
... ...
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK 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.
if building_docs or settings.DATABASE_ENGINE == 'postgresql_psycopg2':
__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>
"""

0 comments on commit 1e1b57b

Please sign in to comment.