Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding transaction rollback to exception_handler #1204

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rest_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django.http import Http404
from django.utils.datastructures import SortedDict
from django.views.decorators.csrf import csrf_exempt
from django.db import transaction

from rest_framework import status, exceptions
from rest_framework.compat import smart_text, HttpResponseBase, View
from rest_framework.request import Request
Expand Down Expand Up @@ -366,6 +368,14 @@ def handle_exception(self, exc):
if response is None:
raise

# We've suppressed the exception but still need to rollback any transaction.
if hasattr(transaction, 'set_rollback'):
# If running in >=1.6 then mark a rollback as required and let Django deal with it.
transaction.set_rollback(True) # Django 1.6+
elif transaction.is_managed():
# If running <=1.5 and TransactionMiddleware is installed then force it's rollback behavior.
TransactionMiddleware().process_exception(request, None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ought to put branching code like this in the compat module.

eg introduce a set_rollback(transaction) function there that performs this behavior.


response.exception = True
return response

Expand Down