python – OperationalError: database is locked

python – OperationalError: database is locked

From django doc:

SQLite is meant to be a lightweight
database, and thus cant support a
high level of concurrency.
OperationalError: database is locked
errors indicate that your application
is experiencing more concurrency than
sqlite can handle in default
configuration. This error means that
one thread or process has an exclusive
lock on the database connection and
another thread timed out waiting for
the lock the be released.

Pythons SQLite wrapper has a default
timeout value that determines how long
the second thread is allowed to wait
on the lock before it times out and
raises the OperationalError: database
is locked error.

If youre getting this error, you can
solve it by:

  • Switching to another database backend. At a certain point SQLite becomes too lite for real-world applications, and these sorts of concurrency errors indicate youve reached that point.
  • Rewriting your code to reduce concurrency and ensure that database transactions are short-lived.
  • Increase the default timeout value by setting the timeout database option

In my case, It was because I open the database from SQLite Browser. When I close it from the browser, the problem is gone.

python – OperationalError: database is locked

The practical reason for this is often that the python or django shells have opened a request to the DB and it wasnt closed properly; killing your terminal access often frees it up. I had this error on running command line tests today.

Edit: I get periodic upvotes on this. If youd like to kill access without rebooting the terminal, then from commandline you can do:

from django import db

Leave a Reply

Your email address will not be published.