Skip to content

Commit 5c65845

Browse files
committed
Revert check for non_atomic_requests, instead rely again on db.in_atomic_block
1 parent 5e5f559 commit 5c65845

File tree

2 files changed

+14
-37
lines changed

2 files changed

+14
-37
lines changed

rest_framework/views.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,13 @@ def get_view_description(view, html=False):
6262
return description
6363

6464

65-
def set_rollback(request):
66-
# We need the actual view func returned by the URL resolver which gets used
67-
# by Django's BaseHandler to determine `non_atomic_requests`. Be cautious
68-
# when fetching it though as it won't be set when views are tested with
69-
# requessts from a RequestFactory.
70-
try:
71-
non_atomic_requests = request.resolver_match.func._non_atomic_requests
72-
except AttributeError:
73-
non_atomic_requests = set()
74-
65+
def set_rollback():
66+
# Rollback all connections that have ATOMIC_REQUESTS set, if it looks their
67+
# @atomic for the request was started
68+
# Note this check in_atomic_block may be a false positive due to
69+
# transactions started another way, e.g. through testing with TestCase
7570
for db in connections.all():
76-
if db.settings_dict['ATOMIC_REQUESTS'] and db.alias not in non_atomic_requests:
71+
if db.settings_dict['ATOMIC_REQUESTS'] and db.in_atomic_block:
7772
transaction.set_rollback(True, using=db.alias)
7873

7974

@@ -104,7 +99,7 @@ def exception_handler(exc, context):
10499
else:
105100
data = {'detail': exc.detail}
106101

107-
set_rollback(context['request'])
102+
set_rollback()
108103
return Response(data, status=exc.status_code, headers=headers)
109104

110105
return None

tests/test_atomic_requests.py

+7-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.conf.urls import url
44
from django.db import connection, connections, transaction
55
from django.http import Http404
6-
from django.test import TestCase, override_settings
6+
from django.test import TestCase, TransactionTestCase, override_settings
77

88
from rest_framework import status
99
from rest_framework.exceptions import APIException
@@ -43,20 +43,8 @@ def get(self, request, *args, **kwargs):
4343
raise Http404
4444

4545

46-
class UrlDecoratedNonAtomicAPIExceptionView(APIView):
47-
def get(self, request, *args, **kwargs):
48-
list(BasicModel.objects.all())
49-
raise Http404
50-
51-
5246
urlpatterns = (
5347
url(r'^non-atomic-exception$', NonAtomicAPIExceptionView.as_view()),
54-
url(
55-
r'^url-decorated-non-atomic-exception$',
56-
transaction.non_atomic_requests(
57-
UrlDecoratedNonAtomicAPIExceptionView.as_view()
58-
),
59-
),
6048
)
6149

6250

@@ -147,25 +135,19 @@ def test_api_exception_rollback_transaction(self):
147135
"'atomic' requires transactions and savepoints."
148136
)
149137
@override_settings(ROOT_URLCONF='tests.test_atomic_requests')
150-
class NonAtomicDBTransactionAPIExceptionTests(TestCase):
138+
class NonAtomicDBTransactionAPIExceptionTests(TransactionTestCase):
151139
def setUp(self):
152140
connections.databases['default']['ATOMIC_REQUESTS'] = True
153141

154-
def tearDown(self):
155-
connections.databases['default']['ATOMIC_REQUESTS'] = False
142+
@self.addCleanup
143+
def restore_atomic_requests():
144+
connections.databases['default']['ATOMIC_REQUESTS'] = False
156145

157146
def test_api_exception_rollback_transaction_non_atomic_view(self):
158147
response = self.client.get('/non-atomic-exception')
159148

149+
# without check for db.in_atomic_block, would raise 500 due to attempt
150+
# to rollback without transaction
160151
assert response.status_code == status.HTTP_404_NOT_FOUND
161-
assert not transaction.get_rollback()
162-
# Check we can still perform DB queries
163-
list(BasicModel.objects.all())
164-
165-
def test_api_exception_rollback_transaction_url_decorated_non_atomic_view(self):
166-
response = self.client.get('/url-decorated-non-atomic-exception')
167-
168-
assert response.status_code == status.HTTP_404_NOT_FOUND
169-
assert not transaction.get_rollback()
170152
# Check we can still perform DB queries
171153
list(BasicModel.objects.all())

0 commit comments

Comments
 (0)