7

By reading the doc: https://docs.djangoproject.com/en/dev/topics/db/transactions/#django.db.transaction.atomic

I know that

atomic blocks can be nested. In this case, when an inner block completes successfully, its effects can still be rolled back if an exception is raised in the outer block at a later point.

However, my question is that for a code structure like following:

@transaction.atomic
def A():
    ## something
    B()
    C()
    ## something

@transaction.atomic
def B(): 
    ## something

@transaction.atomic
def C():
    ## something

If B and C both succeed, and A goes wrong after them, then B and C will right back, right?

What if B succeed, but C messed up, will B get roll back?

And about the memory usage for maintaining this roll back functionality, is there any difference between the one above and the one following:

@transaction.atomic
def A():
    B()
    C()

def B():
    ## something
def C():
    ## something 

I know these two structure handle different case. I am just asking, assume they both succeed (completely), what is the difference at the memory usage level?

Thanks in advance.

Jerry Meng
  • 1,466
  • 3
  • 21
  • 40

1 Answers1

12

If B and C both succeed, and A goes wrong after them, then B and C will right back, right?

Yes, the entire transaction A would be rolled back including B and C.

What if B succeed, but C messed up, will B get roll back?

Again, the entire transaction A would be rolled back, including B and C.

I can't say what the memory usage would be. It would depend on your database engine and the code you are running. Unless you are working with very large datasets, I wouldn't worry about it. If you are working with very large datasets, then experiment!

Alasdair
  • 298,606
  • 55
  • 578
  • 516