Django Nested Transactions - "with transaction.atomic ()" - search for clarifications

The Nested Django transaction - "with transaction.atomic ()" asked the question, given that ...

def functionA():
    with transaction.atomic():
        #save something
        functionB()

def functionB():
    with transaction.atomic():
        #save another thing

If functionBit fails and rolls back, then functionArolls back too?

Kevin Christopher Henry replies: "Yes, if an exception occurs in any of the functions, they will both be rolled back." He then cites documents that state:

atomic blocks can be nested. In this case, when the indoor unit is successfully completed, its effects can still be discarded if an exception is added to the outdoor unit at a later point.

. , INNER BLOCK ( functionB) , , OUTER ( A) . . , INNER (functionB) FAILS, OUTER (functionA) ? .

...

from django.db import IntegrityError, transaction

@transaction.atomic
def viewfunc(request):
    create_parent()

    try:
        with transaction.atomic():
            generate_relationships()
    except IntegrityError:
        handle_exception()

    add_children()

... ...

, generate_relationships() , , add_children(), create_parent() .

, , generate_relationships() ( functionB ) FAIL, , create_parent() add_children(), . , , .

, , / Django, Transaction.atomic.

Django stackoverflow, , , , . - - . .

+6
2

X, Y Z:

with transaction.atomic():
    X
    with transaction.atomic():
        Y
    Z

X , , .

Y - , . , , Python . , . , , .

- , , Z . , X, Y:

, , .

, .

with transaction.atomic():
    X
    try:
        with transaction.atomic():
            Y
    except IntgrityError:
        pass
    Z

, Y , ( ), ( ).

, , , ( ), - .

, .

+3

catching IntegrityError, :

try/except

, , , /

:

. , , , .

, , -

+1

All Articles