Created
August 26, 2012 21:54
-
-
Save k4ml/3483810 to your computer and use it in GitHub Desktop.
Django transaction.commit_manually wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import transaction | |
def transaction_commit_manually_wrapper(func): | |
"""Wrapper to transaction.commit_manually so that we can reraise any | |
uncaught exception instead of getting TransactionManagementError. | |
Ref - https://code.djangoproject.com/ticket/6623 | |
""" | |
@transaction.commit_manually | |
def func_wrapper(*args, **kwargs): | |
try: | |
return func(*args, **kwargs) | |
except Exception as e: | |
transaction.rollback() | |
raise | |
return func_wrapper | |
# Your views then should look like this instead | |
@transaction_commit_manually_wrapper | |
dev my_views(request): | |
try: | |
do_something() | |
except Exception: | |
transaction.rollback() | |
transaction.commit() | |
return HttpResponse("Hello world") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment