Open tannercollin opened 2 years ago
I realize this is an old issue but here is what I see from your issue:
Thanks for the reply. It's not practical to stop my app to run the clean_duplicate_history
command each night because I might miss an important request like a PayPal payment notification. It needs to stay running.
The clean_duplicate_history
was locking the database for too long, which is why I filed this issue.
I believe you're misunderstanding me. I am not recommending you stop your app. I'm pointing out the error is happening because you have two processes writing to a database that does a full lock during write. There is no solution other than migrating off SQLite. This is one of many reasons SQLite is for local development only
Yes, I understand that. My issue described one solution:
I suspect this atomic transaction is what's taking so long. Would it be possible to break it into pieces so that other threads could access the database?
SQLite is actually fine for production use: https://www.sqlite.org/whentouse.html
Each writer is supposed to hold a lock for a short period of time, not many seconds like the clean_duplicate_history
command. Other writers queue up and wait their turn. The "database is locked" exception is raised when another writer waits for longer than a set timeout (in my case 20 seconds).
Note that the Django docs specifically call this out:
Performance considerations
Open transactions have a performance cost for your database server. To minimize this overhead, keep your transactions as short as possible. This is especially important if you’re using atomic() in long-running processes, outside of Django’s request / response cycle.
I noticed every once in a while my app would log a "database is locked" error:
When it happened, it was always at the 45th minute. This is when I run the
clean_duplicate_history
command with cron:This is a difficult situation because:
timeout
to 20 seconds. I don't want to increase it further because I don't want requests to take that long.I suspect this atomic transaction is what's taking so long. Would it be possible to break it into pieces so that other threads could access the database?
Or is there a different solution you guys recommend? Thanks!