Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django nested Transaction.atomic

Tags:

python

django

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.

like image 389
Jerry Meng Avatar asked Jan 15 '15 21:01

Jerry Meng


People also ask

What does transaction atomic do in Django?

atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.

Are transactions atomic?

SQL transactions, like transactions on all database platforms, put the data in isolation to cover the entire ACID acronym (atomic, consistent, isolated and durable). So the answer is yes.

What is atomic transaction in SOA?

An atomic transaction is a single, irreducible component of a classic transaction, such as making a purchase. WS-AT ensures that if a single atomic transaction fails, the whole transaction fails: A partial transaction cannot take place.

Can't execute queries until the end of the atomic block?

You can't execute queries until the end of the 'atomic' block." is raised when you try to used a database connection after a database exception even those autocommit was set to false from the start. It should be up to the user how to handle the database exception and the transaction as autocommit was set to false.


1 Answers

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!

like image 166
Alasdair Avatar answered Sep 22 '22 00:09

Alasdair