Catching errors inside transaction might lead inconsistent state if the
database if error handling swallows exception.

Reraising exception or like you want to do raise new exception it would
work as expected.

Nested atomic blocks can be used to safely rollback state of database if
you want to handle smaller units of transactiond.


13.6.2017 17.52 "Alex Krupp" <alex.kr...@gmail.com> kirjoitti:

The Django documentation gives a warning
<https://docs.djangoproject.com/en/1.11/topics/db/transactions/#controlling-transactions-explicitly>
to avoid catching errors inside transaction.atomic() blocks, and to use
nested transactions if you need to do so. But in the case where we have
code like:

with transaction.atomic():
    # Create and save some models here

    try:
        SomeModel.objects.get(id=NON_EXISTENT_ID)
    except SomeModel.DoesNotExist:
        raise SomeCustomError()

Will anything bad actually happen if we just immediately raise a custom
error without doing any other error handling? The expected behavior in this
case would be that the entire transaction gets rolled back, and so nothing
before or after the exception is committed.

I'm just wondering in cases like these there is any reason for using the
recommended nested transaction, or if it's just extra code that's not
serving any purpose. The examples only speak to cases where there is there
would otherwise be database queries getting executed in between the first
database error and the end of the transaction, which isn't the case here.

-- 
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/
msgid/django-users/8765cfc1-6083-4f6e-8f86-ace888a0ad21%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/8765cfc1-6083-4f6e-8f86-ace888a0ad21%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91odHQpca2ZLeaFefH8%2BNoXOfkwjW4qZJcaqyqs2wgd5JyA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to