Setup:
- Python script A inserts data to a database every 15 minutes
- Python script B queries a few of the latest entries from the database every few minutes
Both use Django’s ORM, run on the same machine and use a local MySQL database.
The Problem:
B fetches entries, except for the latest one, even though A saves it minutes before.
I suspected that A doesn’t close the transaction, thus B sees the database without the last entry. Indeed when examining the MySQL logs, I noticed the commit for each INSERT happens right before the next INSERT.
Even though it’s supposed to be redundant, I added @commit_on_success decorator to the A function that includes the save(), but it did not help.
How can I force Django (or MySQL?!) to commit right after the save()?
UPDATE:
I discovered that the commits DO happen – I was mislead to believe they don’t because MySQL’s General Query Log only has 1 sec resolution.
In light of this and other new information, I’ve reasked the question here.
You can use the
commit_manuallydecorator and call it whenever you want.Straight from the documentation:
This answers the question you asked, though I wonder if there might be something else at work.
NOTE:
commit_manuallywas deprecated in 1.6 and removed in 1.8.