diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2010-03-12 14:10:01 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2010-03-12 14:10:01 +0000 |
| commit | ef0be292a687f95f1081b2ff8170fd7cbd5a365f (patch) | |
| tree | 6d731bf5ca7c5eeec5821125a3af3310044cff64 | |
| parent | 615eab6b0255608e2094f0ecf02c5351a1e887cc (diff) | |
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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12764 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/db/transaction.py | 6 | ||||
| -rw-r--r-- | tests/modeltests/transactions/models.py | 21 |
2 files changed, 26 insertions, 1 deletions
diff --git a/django/db/transaction.py b/django/db/transaction.py index 6cd078d94d..31d717f4d2 100644 --- a/django/db/transaction.py +++ b/django/db/transaction.py @@ -304,7 +304,11 @@ def commit_on_success(using=None): 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) diff --git a/tests/modeltests/transactions/models.py b/tests/modeltests/transactions/models.py index 0c1cd21a04..3ae1290b4a 100644 --- a/tests/modeltests/transactions/models.py +++ b/tests/modeltests/transactions/models.py @@ -130,3 +130,24 @@ Traceback (most recent call last): 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> + +""" |
