Make django commit

Tune:

  • Python A script inserts data into the database every 15 minutes
  • Python B script retrieves the last few records from the database every few minutes

Both use Django ORM, run on the same computer, and use the local MySQL database.

This problem:
B retrieves records except the most recent, although A saves them minutes earlier.

I suspected that A did not close the transaction, so B sees the database without the last record. Indeed, while studying MySQL logs, I noticed that commit for each INSERT happens right before the next INSERT .

Although it should be redundant, I added the @commit_on_success decorator to function A, which includes save() , but that didn't help.

How can I get Django (or MySQL ?!) to commit right after save() ?

UPDATE:
I found that commits really happen - I mistakenly thought this was not the case, because MySQL General Query Log only has a resolution of 1 second .
In the light of this and other new information, I again asked a question here .

+8
source share
2 answers

You can use the commit_manually decorator and call it whenever you want.

Directly from the documentation :

 from django.db import transaction @transaction.commit_manually def viewfunc(request): ... # You can commit/rollback however and whenever you want transaction.commit() ... # But you've got to remember to do it yourself! try: ... except: transaction.rollback() else: transaction.commit() 

This answers the question that you asked, although I wonder if there could be anything else at work.

NOTE: commit_manually deprecated in 1.6 and removed in 1.8 .

+10
source

The problem is because MySQL defaults to the REPEATABLE-READ transaction isolation level. This means that the same query should return the same value. Changes will not return. You can do two things here:

  1. Set the isolation level of the READ-COMMITTED transaction in the settings. As explained here .
  2. Forced commit, so close the transaction in script B so that when you start a new transaction, you will see all the changes before this new transaction. Model.objects.update() does the Model.objects.update() thing.
0
source

Source: https://habr.com/ru/post/894819/


All Articles