Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #14550 -- fixed the behavior of commit_on_success to exit the t…

…ransaction properly. This was a bug introduced in [14288]. Thanks to Justin for the report and Florian Apolloner for help debugging.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14343 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 01e2be557bd41134e182a26bd705ff2039cef2b9 1 parent eccc42a
@alex alex authored
View
21 django/db/transaction.py
@@ -345,16 +345,19 @@ def entering(using):
managed(True, using=using)
def exiting(exc_value, using):
- if exc_value is not None:
- if is_dirty(using=using):
- rollback(using=using)
- else:
- if is_dirty(using=using):
- try:
- commit(using=using)
- except:
+ try:
+ if exc_value is not None:
+ if is_dirty(using=using):
rollback(using=using)
- raise
+ else:
+ if is_dirty(using=using):
+ try:
+ commit(using=using)
+ except:
+ rollback(using=using)
+ raise
+ finally:
+ leave_transaction_management(using=using)
return _transaction_func(entering, exiting, using)
View
20 tests/modeltests/transactions/tests.py
@@ -114,6 +114,25 @@ def test_commit_on_success_succeed(self):
self.assertEqual(list(Reporter.objects.all()), [])
@skipUnlessDBFeature('supports_transactions')
+ def test_commit_on_success_exit(self):
+ @transaction.autocommit()
+ def gen_reporter():
+ @transaction.commit_on_success
+ def create_reporter():
+ Reporter.objects.create(first_name="Bobby", last_name="Tables")
+
+ create_reporter()
+ # Much more formal
+ r = Reporter.objects.get()
+ r.first_name = "Robert"
+ r.save()
+
+ gen_reporter()
+ r = Reporter.objects.get()
+ self.assertEqual(r.first_name, "Robert")
+
+
+ @skipUnlessDBFeature('supports_transactions')
def test_manually_managed(self):
"""
You can manually manage transactions if you really want to, but you
@@ -146,6 +165,7 @@ def test_manually_managed_with_using(self):
using_manually_managed_mistake
)
+
class TransactionRollbackTests(TransactionTestCase):
def execute_bad_sql(self):
cursor = connection.cursor()
View
14 tests/modeltests/transactions/tests_25.py
@@ -79,6 +79,20 @@ def test_commit_on_success_succeed(self):
self.assertQuerysetEqual(Reporter.objects.all(), [])
@skipUnlessDBFeature('supports_transactions')
+ def test_commit_on_success_exit(self):
+ with transaction.autocommit():
+ with transaction.commit_on_success():
+ Reporter.objects.create(first_name="Bobby", last_name="Tables")
+
+ # Much more formal
+ r = Reporter.objects.get()
+ r.first_name = "Robert"
+ r.save()
+
+ r = Reporter.objects.get()
+ self.assertEqual(r.first_name, "Robert")
+
+ @skipUnlessDBFeature('supports_transactions')
def test_manually_managed(self):
"""
You can manually manage transactions if you really want to, but you
Please sign in to comment.
Something went wrong with that request. Please try again.