2013-03-05 05:17:35 +08:00
|
|
|
import sys
|
2015-01-28 22:55:52 +08:00
|
|
|
import threading
|
2014-03-24 03:45:22 +08:00
|
|
|
import time
|
2013-07-01 20:22:27 +08:00
|
|
|
from unittest import skipIf, skipUnless
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import (
|
|
|
|
DatabaseError,
|
|
|
|
Error,
|
|
|
|
IntegrityError,
|
|
|
|
OperationalError,
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
)
|
|
|
|
from django.test import (
|
2020-11-23 00:20:56 +08:00
|
|
|
TestCase,
|
|
|
|
TransactionTestCase,
|
|
|
|
skipIfDBFeature,
|
|
|
|
skipUnlessDBFeature,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2010-09-13 13:28:29 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from .models import Reporter
|
2010-09-13 13:28:29 +08:00
|
|
|
|
|
|
|
|
2018-10-04 22:09:06 +08:00
|
|
|
@skipUnlessDBFeature("uses_savepoints")
|
2013-03-05 05:17:35 +08:00
|
|
|
class AtomicTests(TransactionTestCase):
|
|
|
|
"""
|
|
|
|
Tests for the atomic decorator and context manager.
|
|
|
|
|
|
|
|
The tests make assertions on internal attributes because there isn't a
|
|
|
|
robust way to ask the database for its current transaction state.
|
|
|
|
|
|
|
|
Since the decorator syntax is converted into a context manager (see the
|
|
|
|
implementation), there are only a few basic tests with the decorator
|
|
|
|
syntax and the bulk of the tests use the context manager syntax.
|
|
|
|
"""
|
|
|
|
|
2013-06-04 14:09:29 +08:00
|
|
|
available_apps = ["transactions"]
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
def test_decorator_syntax_commit(self):
|
|
|
|
@transaction.atomic
|
|
|
|
def make_reporter():
|
2020-10-19 00:29:52 +08:00
|
|
|
return Reporter.objects.create(first_name="Tintin")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = make_reporter()
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_decorator_syntax_rollback(self):
|
|
|
|
@transaction.atomic
|
|
|
|
def make_reporter():
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-03-05 05:17:35 +08:00
|
|
|
make_reporter()
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_alternate_decorator_syntax_commit(self):
|
|
|
|
@transaction.atomic()
|
|
|
|
def make_reporter():
|
2020-10-19 00:29:52 +08:00
|
|
|
return Reporter.objects.create(first_name="Tintin")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = make_reporter()
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_alternate_decorator_syntax_rollback(self):
|
|
|
|
@transaction.atomic()
|
|
|
|
def make_reporter():
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-03-05 05:17:35 +08:00
|
|
|
make_reporter()
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_commit(self):
|
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin")
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_rollback(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_nested_commit_commit(self):
|
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter1 = Reporter.objects.create(first_name="Tintin")
|
2013-03-05 05:17:35 +08:00
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter2 = Reporter.objects.create(
|
|
|
|
first_name="Archibald", last_name="Haddock"
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_nested_commit_rollback(self):
|
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_nested_rollback_commit(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-03-05 05:17:35 +08:00
|
|
|
with transaction.atomic():
|
2013-09-22 19:49:46 +08:00
|
|
|
Reporter.objects.create(last_name="Tintin")
|
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(last_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his first name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_nested_rollback_rollback(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(last_name="Tintin")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
|
|
|
raise Exception("Oops, that's his first name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
def test_merged_commit_commit(self):
|
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter1 = Reporter.objects.create(first_name="Tintin")
|
2013-03-08 22:44:41 +08:00
|
|
|
with transaction.atomic(savepoint=False):
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter2 = Reporter.objects.create(
|
|
|
|
first_name="Archibald", last_name="Haddock"
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
|
2013-03-08 22:44:41 +08:00
|
|
|
|
|
|
|
def test_merged_commit_rollback(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Tintin")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic(savepoint=False):
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
2013-03-08 22:44:41 +08:00
|
|
|
# Writes in the outer block are rolled back too.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-08 22:44:41 +08:00
|
|
|
|
|
|
|
def test_merged_rollback_commit(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(last_name="Tintin")
|
|
|
|
with transaction.atomic(savepoint=False):
|
|
|
|
Reporter.objects.create(last_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his first name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-08 22:44:41 +08:00
|
|
|
|
|
|
|
def test_merged_rollback_rollback(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(last_name="Tintin")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic(savepoint=False):
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
|
|
|
raise Exception("Oops, that's his first name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-08 22:44:41 +08:00
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
def test_reuse_commit_commit(self):
|
|
|
|
atomic = transaction.atomic()
|
|
|
|
with atomic:
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter1 = Reporter.objects.create(first_name="Tintin")
|
2013-03-05 05:17:35 +08:00
|
|
|
with atomic:
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter2 = Reporter.objects.create(
|
|
|
|
first_name="Archibald", last_name="Haddock"
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_reuse_commit_rollback(self):
|
|
|
|
atomic = transaction.atomic()
|
|
|
|
with atomic:
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with atomic:
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_reuse_rollback_commit(self):
|
|
|
|
atomic = transaction.atomic()
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-03-05 05:17:35 +08:00
|
|
|
with atomic:
|
2013-09-22 19:49:46 +08:00
|
|
|
Reporter.objects.create(last_name="Tintin")
|
|
|
|
with atomic:
|
|
|
|
Reporter.objects.create(last_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his first name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
def test_reuse_rollback_rollback(self):
|
|
|
|
atomic = transaction.atomic()
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with atomic:
|
|
|
|
Reporter.objects.create(last_name="Tintin")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with atomic:
|
|
|
|
Reporter.objects.create(first_name="Haddock")
|
|
|
|
raise Exception("Oops, that's his last name")
|
|
|
|
raise Exception("Oops, that's his first name")
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-06-28 04:19:54 +08:00
|
|
|
def test_force_rollback(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Tintin")
|
|
|
|
# atomic block shouldn't rollback, but force it.
|
|
|
|
self.assertFalse(transaction.get_rollback())
|
|
|
|
transaction.set_rollback(True)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-06-28 04:19:54 +08:00
|
|
|
|
|
|
|
def test_prevent_rollback(self):
|
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin")
|
2013-06-28 04:19:54 +08:00
|
|
|
sid = transaction.savepoint()
|
|
|
|
# trigger a database error inside an inner atomic without savepoint
|
2013-09-22 19:49:46 +08:00
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
with transaction.atomic(savepoint=False):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT no_such_col FROM transactions_reporter")
|
2013-09-23 04:14:17 +08:00
|
|
|
# prevent atomic from rolling back since we're recovering manually
|
2013-06-28 04:19:54 +08:00
|
|
|
self.assertTrue(transaction.get_rollback())
|
|
|
|
transaction.set_rollback(False)
|
2013-09-23 04:14:17 +08:00
|
|
|
transaction.savepoint_rollback(sid)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-06-28 04:19:54 +08:00
|
|
|
|
2022-12-03 10:04:42 +08:00
|
|
|
@skipUnlessDBFeature("can_release_savepoints")
|
|
|
|
def test_failure_on_exit_transaction(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(last_name="Tintin")
|
|
|
|
self.assertEqual(len(Reporter.objects.all()), 1)
|
|
|
|
# Incorrect savepoint id to provoke a database error.
|
|
|
|
connection.savepoint_ids.append("12")
|
|
|
|
with self.assertRaises(transaction.TransactionManagementError):
|
|
|
|
len(Reporter.objects.all())
|
|
|
|
self.assertIs(connection.needs_rollback, True)
|
|
|
|
if connection.savepoint_ids:
|
|
|
|
connection.savepoint_ids.pop()
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
class AtomicInsideTransactionTests(AtomicTests):
|
|
|
|
"""All basic tests for atomic should also pass within an existing transaction."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.atomic = transaction.atomic()
|
|
|
|
self.atomic.__enter__()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.atomic.__exit__(*sys.exc_info())
|
|
|
|
|
|
|
|
|
2013-03-13 21:08:32 +08:00
|
|
|
class AtomicWithoutAutocommitTests(AtomicTests):
|
|
|
|
"""All basic tests for atomic should also pass when autocommit is turned off."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
transaction.set_autocommit(False)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# The tests access the database after exercising 'atomic', initiating
|
|
|
|
# a transaction ; a rollback is required before restoring autocommit.
|
|
|
|
transaction.rollback()
|
|
|
|
transaction.set_autocommit(True)
|
|
|
|
|
|
|
|
|
2018-10-04 22:09:06 +08:00
|
|
|
@skipUnlessDBFeature("uses_savepoints")
|
2013-03-08 22:44:41 +08:00
|
|
|
class AtomicMergeTests(TransactionTestCase):
|
|
|
|
"""Test merging transactions with savepoint=False."""
|
|
|
|
|
2013-06-04 14:09:29 +08:00
|
|
|
available_apps = ["transactions"]
|
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
def test_merged_outer_rollback(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Tintin")
|
|
|
|
with transaction.atomic(savepoint=False):
|
|
|
|
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic(savepoint=False):
|
2013-09-23 03:20:22 +08:00
|
|
|
Reporter.objects.create(first_name="Calculus")
|
2013-09-22 19:49:46 +08:00
|
|
|
raise Exception("Oops, that's his last name")
|
2013-09-23 04:14:17 +08:00
|
|
|
# The third insert couldn't be roll back. Temporarily mark the
|
|
|
|
# connection as not needing rollback to check it.
|
|
|
|
self.assertTrue(transaction.get_rollback())
|
|
|
|
transaction.set_rollback(False)
|
2013-03-08 22:44:41 +08:00
|
|
|
self.assertEqual(Reporter.objects.count(), 3)
|
2013-09-23 04:14:17 +08:00
|
|
|
transaction.set_rollback(True)
|
|
|
|
# The second insert couldn't be roll back. Temporarily mark the
|
|
|
|
# connection as not needing rollback to check it.
|
|
|
|
self.assertTrue(transaction.get_rollback())
|
|
|
|
transaction.set_rollback(False)
|
2013-03-08 22:44:41 +08:00
|
|
|
self.assertEqual(Reporter.objects.count(), 3)
|
2013-09-23 04:14:17 +08:00
|
|
|
transaction.set_rollback(True)
|
|
|
|
# The first block has a savepoint and must roll back.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [])
|
2013-03-08 22:44:41 +08:00
|
|
|
|
|
|
|
def test_merged_inner_savepoint_rollback(self):
|
|
|
|
with transaction.atomic():
|
2020-10-19 00:29:52 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin")
|
2013-03-08 22:44:41 +08:00
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic(savepoint=False):
|
2013-09-23 03:20:22 +08:00
|
|
|
Reporter.objects.create(first_name="Calculus")
|
2013-09-22 19:49:46 +08:00
|
|
|
raise Exception("Oops, that's his last name")
|
2013-09-23 04:14:17 +08:00
|
|
|
# The third insert couldn't be roll back. Temporarily mark the
|
|
|
|
# connection as not needing rollback to check it.
|
|
|
|
self.assertTrue(transaction.get_rollback())
|
|
|
|
transaction.set_rollback(False)
|
2013-03-08 22:44:41 +08:00
|
|
|
self.assertEqual(Reporter.objects.count(), 3)
|
2013-09-23 04:14:17 +08:00
|
|
|
transaction.set_rollback(True)
|
|
|
|
# The second block has a savepoint and must roll back.
|
2013-03-08 22:44:41 +08:00
|
|
|
self.assertEqual(Reporter.objects.count(), 1)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter])
|
2013-03-08 22:44:41 +08:00
|
|
|
|
|
|
|
|
2018-10-04 22:09:06 +08:00
|
|
|
@skipUnlessDBFeature("uses_savepoints")
|
2013-03-05 06:26:31 +08:00
|
|
|
class AtomicErrorsTests(TransactionTestCase):
|
2013-09-23 04:14:17 +08:00
|
|
|
available_apps = ["transactions"]
|
2017-05-29 03:37:21 +08:00
|
|
|
forbidden_atomic_msg = "This is forbidden when an 'atomic' block is active."
|
2013-06-04 14:09:29 +08:00
|
|
|
|
2013-03-13 21:08:32 +08:00
|
|
|
def test_atomic_prevents_setting_autocommit(self):
|
2013-03-05 06:26:31 +08:00
|
|
|
autocommit = transaction.get_autocommit()
|
2013-09-22 19:49:46 +08:00
|
|
|
with transaction.atomic():
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
transaction.TransactionManagementError, self.forbidden_atomic_msg
|
|
|
|
):
|
2013-09-22 19:49:46 +08:00
|
|
|
transaction.set_autocommit(not autocommit)
|
2013-03-05 06:26:31 +08:00
|
|
|
# Make sure autocommit wasn't changed.
|
|
|
|
self.assertEqual(connection.autocommit, autocommit)
|
|
|
|
|
|
|
|
def test_atomic_prevents_calling_transaction_methods(self):
|
|
|
|
with transaction.atomic():
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
transaction.TransactionManagementError, self.forbidden_atomic_msg
|
|
|
|
):
|
2013-03-05 06:26:31 +08:00
|
|
|
transaction.commit()
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
transaction.TransactionManagementError, self.forbidden_atomic_msg
|
|
|
|
):
|
2013-03-05 06:26:31 +08:00
|
|
|
transaction.rollback()
|
|
|
|
|
2013-09-23 04:14:17 +08:00
|
|
|
def test_atomic_prevents_queries_in_broken_transaction(self):
|
|
|
|
r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
|
|
|
with transaction.atomic():
|
|
|
|
r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
r2.save(force_insert=True)
|
|
|
|
# The transaction is marked as needing rollback.
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"An error occurred in the current transaction. You can't "
|
|
|
|
"execute queries until the end of the 'atomic' block."
|
|
|
|
)
|
2022-10-03 00:53:05 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
transaction.TransactionManagementError, msg
|
|
|
|
) as cm:
|
2013-09-23 04:14:17 +08:00
|
|
|
r2.save(force_update=True)
|
2022-10-03 00:53:05 +08:00
|
|
|
self.assertIsInstance(cm.exception.__cause__, IntegrityError)
|
2013-09-23 04:14:17 +08:00
|
|
|
self.assertEqual(Reporter.objects.get(pk=r1.pk).last_name, "Haddock")
|
|
|
|
|
|
|
|
@skipIfDBFeature("atomic_transactions")
|
|
|
|
def test_atomic_allows_queries_after_fixing_transaction(self):
|
|
|
|
r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
|
|
|
with transaction.atomic():
|
|
|
|
r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
r2.save(force_insert=True)
|
|
|
|
# Mark the transaction as no longer needing rollback.
|
|
|
|
transaction.set_rollback(False)
|
|
|
|
r2.save(force_update=True)
|
|
|
|
self.assertEqual(Reporter.objects.get(pk=r1.pk).last_name, "Calculus")
|
|
|
|
|
2014-03-24 04:45:31 +08:00
|
|
|
@skipUnlessDBFeature("test_db_allows_multiple_connections")
|
|
|
|
def test_atomic_prevents_queries_in_broken_transaction_after_client_close(self):
|
|
|
|
with transaction.atomic():
|
|
|
|
Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
|
|
|
connection.close()
|
|
|
|
# The connection is closed and the transaction is marked as
|
|
|
|
# needing rollback. This will raise an InterfaceError on databases
|
|
|
|
# that refuse to create cursors on closed connections (PostgreSQL)
|
|
|
|
# and a TransactionManagementError on other databases.
|
|
|
|
with self.assertRaises(Error):
|
|
|
|
Reporter.objects.create(first_name="Cuthbert", last_name="Calculus")
|
|
|
|
# The connection is usable again .
|
|
|
|
self.assertEqual(Reporter.objects.count(), 0)
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
|
2022-04-14 15:53:31 +08:00
|
|
|
@skipUnlessDBFeature("uses_savepoints")
|
2014-03-24 03:45:22 +08:00
|
|
|
@skipUnless(connection.vendor == "mysql", "MySQL-specific behaviors")
|
|
|
|
class AtomicMySQLTests(TransactionTestCase):
|
|
|
|
available_apps = ["transactions"]
|
|
|
|
|
|
|
|
@skipIf(threading is None, "Test requires threading")
|
|
|
|
def test_implicit_savepoint_rollback(self):
|
|
|
|
"""MySQL implicitly rolls back savepoints when it deadlocks (#22291)."""
|
2017-02-02 04:34:17 +08:00
|
|
|
Reporter.objects.create(id=1)
|
|
|
|
Reporter.objects.create(id=2)
|
2014-03-24 03:45:22 +08:00
|
|
|
|
2017-02-02 04:34:17 +08:00
|
|
|
main_thread_ready = threading.Event()
|
2014-03-24 03:45:22 +08:00
|
|
|
|
|
|
|
def other_thread():
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
2017-02-02 04:34:17 +08:00
|
|
|
Reporter.objects.select_for_update().get(id=1)
|
|
|
|
main_thread_ready.wait()
|
|
|
|
# 1) This line locks... (see below for 2)
|
2014-03-24 03:45:22 +08:00
|
|
|
Reporter.objects.exclude(id=1).update(id=2)
|
|
|
|
finally:
|
|
|
|
# This is the thread-local connection, not the main connection.
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
other_thread = threading.Thread(target=other_thread)
|
|
|
|
other_thread.start()
|
|
|
|
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(OperationalError, "Deadlock found"):
|
2014-03-24 03:45:22 +08:00
|
|
|
# Double atomic to enter a transaction and create a savepoint.
|
|
|
|
with transaction.atomic():
|
|
|
|
with transaction.atomic():
|
2017-02-02 04:34:17 +08:00
|
|
|
Reporter.objects.select_for_update().get(id=2)
|
|
|
|
main_thread_ready.set()
|
|
|
|
# The two threads can't be synchronized with an event here
|
|
|
|
# because the other thread locks. Sleep for a little while.
|
|
|
|
time.sleep(1)
|
|
|
|
# 2) ... and this line deadlocks. (see above for 1)
|
|
|
|
Reporter.objects.exclude(id=2).update(id=1)
|
2014-03-24 03:45:22 +08:00
|
|
|
|
|
|
|
other_thread.join()
|
|
|
|
|
|
|
|
|
2013-03-12 17:52:16 +08:00
|
|
|
class AtomicMiscTests(TransactionTestCase):
|
2018-09-27 15:45:10 +08:00
|
|
|
available_apps = ["transactions"]
|
2013-06-04 14:09:29 +08:00
|
|
|
|
2013-03-12 17:52:16 +08:00
|
|
|
def test_wrap_callable_instance(self):
|
2015-09-22 02:53:10 +08:00
|
|
|
"""#20028 -- Atomic must support wrapping callable instances."""
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Callable:
|
2013-03-12 17:52:16 +08:00
|
|
|
def __call__(self):
|
|
|
|
pass
|
2015-09-22 02:53:10 +08:00
|
|
|
|
2013-03-12 17:52:16 +08:00
|
|
|
# Must not raise an exception
|
|
|
|
transaction.atomic(Callable())
|
2014-07-28 20:30:41 +08:00
|
|
|
|
|
|
|
@skipUnlessDBFeature("can_release_savepoints")
|
|
|
|
def test_atomic_does_not_leak_savepoints_on_failure(self):
|
2015-09-22 02:53:10 +08:00
|
|
|
"""#23074 -- Savepoints must be released after rollback."""
|
2014-07-28 20:30:41 +08:00
|
|
|
|
|
|
|
# Expect an error when rolling back a savepoint that doesn't exist.
|
|
|
|
# Done outside of the transaction block to ensure proper recovery.
|
|
|
|
with self.assertRaises(Error):
|
|
|
|
# Start a plain transaction.
|
|
|
|
with transaction.atomic():
|
|
|
|
# Swallow the intentional error raised in the sub-transaction.
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
2014-07-28 20:30:41 +08:00
|
|
|
# Start a sub-transaction with a savepoint.
|
|
|
|
with transaction.atomic():
|
|
|
|
sid = connection.savepoint_ids[-1]
|
|
|
|
raise Exception("Oops")
|
|
|
|
|
|
|
|
# This is expected to fail because the savepoint no longer exists.
|
|
|
|
connection.savepoint_rollback(sid)
|
2015-09-22 02:53:10 +08:00
|
|
|
|
2018-09-27 15:45:10 +08:00
|
|
|
def test_mark_for_rollback_on_error_in_transaction(self):
|
|
|
|
with transaction.atomic(savepoint=False):
|
|
|
|
# Swallow the intentional error raised.
|
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
|
|
|
# Wrap in `mark_for_rollback_on_error` to check if the
|
|
|
|
# transaction is marked broken.
|
|
|
|
with transaction.mark_for_rollback_on_error():
|
|
|
|
# Ensure that we are still in a good state.
|
|
|
|
self.assertFalse(transaction.get_rollback())
|
|
|
|
|
|
|
|
raise Exception("Oops")
|
|
|
|
|
|
|
|
# mark_for_rollback_on_error marked the transaction as broken …
|
|
|
|
self.assertTrue(transaction.get_rollback())
|
|
|
|
|
|
|
|
# … and further queries fail.
|
|
|
|
msg = "You can't execute queries until the end of the 'atomic' block."
|
|
|
|
with self.assertRaisesMessage(transaction.TransactionManagementError, msg):
|
|
|
|
Reporter.objects.create()
|
|
|
|
|
|
|
|
# Transaction errors are reset at the end of an transaction, so this
|
|
|
|
# should just work.
|
|
|
|
Reporter.objects.create()
|
|
|
|
|
|
|
|
def test_mark_for_rollback_on_error_in_autocommit(self):
|
|
|
|
self.assertTrue(transaction.get_autocommit())
|
|
|
|
|
|
|
|
# Swallow the intentional error raised.
|
|
|
|
with self.assertRaisesMessage(Exception, "Oops"):
|
|
|
|
# Wrap in `mark_for_rollback_on_error` to check if the transaction
|
|
|
|
# is marked broken.
|
|
|
|
with transaction.mark_for_rollback_on_error():
|
|
|
|
# Ensure that we are still in a good state.
|
|
|
|
self.assertFalse(transaction.get_connection().needs_rollback)
|
|
|
|
|
|
|
|
raise Exception("Oops")
|
|
|
|
|
|
|
|
# Ensure that `mark_for_rollback_on_error` did not mark the transaction
|
|
|
|
# as broken, since we are in autocommit mode …
|
|
|
|
self.assertFalse(transaction.get_connection().needs_rollback)
|
|
|
|
|
|
|
|
# … and further queries work nicely.
|
|
|
|
Reporter.objects.create()
|
|
|
|
|
2016-11-23 21:42:19 +08:00
|
|
|
|
|
|
|
class NonAutocommitTests(TransactionTestCase):
|
|
|
|
available_apps = []
|
|
|
|
|
2021-07-09 13:38:31 +08:00
|
|
|
def setUp(self):
|
|
|
|
transaction.set_autocommit(False)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
transaction.rollback()
|
|
|
|
transaction.set_autocommit(True)
|
|
|
|
|
2016-11-23 21:42:19 +08:00
|
|
|
def test_orm_query_after_error_and_rollback(self):
|
|
|
|
"""
|
|
|
|
ORM queries are allowed after an error and a rollback in non-autocommit
|
|
|
|
mode (#27504).
|
|
|
|
"""
|
|
|
|
r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
|
|
|
|
r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
r2.save(force_insert=True)
|
|
|
|
transaction.rollback()
|
|
|
|
Reporter.objects.last()
|
|
|
|
|
2015-09-22 02:53:10 +08:00
|
|
|
def test_orm_query_without_autocommit(self):
|
|
|
|
"""#24921 -- ORM queries must be possible after set_autocommit(False)."""
|
2021-07-09 13:38:31 +08:00
|
|
|
Reporter.objects.create(first_name="Tintin")
|
2020-11-23 00:20:56 +08:00
|
|
|
|
|
|
|
|
2021-10-01 01:13:56 +08:00
|
|
|
class DurableTestsBase:
|
2020-11-23 00:20:56 +08:00
|
|
|
available_apps = ["transactions"]
|
|
|
|
|
|
|
|
def test_commit(self):
|
|
|
|
with transaction.atomic(durable=True):
|
|
|
|
reporter = Reporter.objects.create(first_name="Tintin")
|
|
|
|
self.assertEqual(Reporter.objects.get(), reporter)
|
|
|
|
|
|
|
|
def test_nested_outer_durable(self):
|
|
|
|
with transaction.atomic(durable=True):
|
|
|
|
reporter1 = Reporter.objects.create(first_name="Tintin")
|
|
|
|
with transaction.atomic():
|
|
|
|
reporter2 = Reporter.objects.create(
|
|
|
|
first_name="Archibald",
|
|
|
|
last_name="Haddock",
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
|
|
|
|
|
|
|
|
def test_nested_both_durable(self):
|
|
|
|
msg = "A durable atomic block cannot be nested within another atomic block."
|
|
|
|
with transaction.atomic(durable=True):
|
|
|
|
with self.assertRaisesMessage(RuntimeError, msg):
|
|
|
|
with transaction.atomic(durable=True):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_nested_inner_durable(self):
|
|
|
|
msg = "A durable atomic block cannot be nested within another atomic block."
|
|
|
|
with transaction.atomic():
|
|
|
|
with self.assertRaisesMessage(RuntimeError, msg):
|
|
|
|
with transaction.atomic(durable=True):
|
|
|
|
pass
|
|
|
|
|
2021-10-01 01:13:56 +08:00
|
|
|
def test_sequence_of_durables(self):
|
2020-11-23 00:20:56 +08:00
|
|
|
with transaction.atomic(durable=True):
|
2021-10-01 01:13:56 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin 1")
|
|
|
|
self.assertEqual(Reporter.objects.get(first_name="Tintin 1"), reporter)
|
2020-11-23 00:20:56 +08:00
|
|
|
with transaction.atomic(durable=True):
|
2021-10-01 01:13:56 +08:00
|
|
|
reporter = Reporter.objects.create(first_name="Tintin 2")
|
|
|
|
self.assertEqual(Reporter.objects.get(first_name="Tintin 2"), reporter)
|
2020-11-23 00:20:56 +08:00
|
|
|
|
|
|
|
|
2021-10-01 01:13:56 +08:00
|
|
|
class DurableTransactionTests(DurableTestsBase, TransactionTestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DurableTests(DurableTestsBase, TestCase):
|
|
|
|
pass
|