Ababababa
Ababababa

Reputation: 362

Django commits to database before transaction ends

I am using Django 1.10 with Postgres database and django-rest-framework. I debug my code and wherever I have some_entity.save(), I immediately see the changes in database. The thing is that I have ATOMIC_REQUESTS = True and this would mean that nothing gets committed to the database until view successfully finishes. I am worried about the database integrity now- if later an exception occurs, nothing gets rolled back

Upvotes: 0

Views: 1040

Answers (1)

Adrien Matissart
Adrien Matissart

Reputation: 1690

What you describe is a perfectly expected behavior with database transactions.

With ATOMIC_REQUESTS = True each request is wrapped in a database transaction, meaning a transaction is started when the request is received and is committed after a response is produced without problem.

During the transaction, you can read your own writes: that's how SQL databases work (and the contrary would be very confusing).

In most configurations though, these writes are "isolated": if they are not committed yet, they will not be visible from a concurrent request (in a distinct transaction). For more details about Transaction Isolation, look at postgresql docs: https://www.postgresql.org/docs/current/static/transaction-iso.html

Upvotes: 1

Related Questions