Sqlite3 Database Is Locked in Azure

OperationalError: database is locked when deploying site to Azure

It seems like a duplication of this question: OperationalError: database is locked.

From the documentation of Django:
https://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errorsoption

SQLite is meant to be a lightweight database, and thus can’t 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.

Python’s 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 you’re 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 you’ve 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

I have been developing a Django web app as well, and I chose a Azure SQL Server to be the database of the application. Everything has been working OK.

Sqlite DB Locked on Azure Dotnet Core Entity Framework

A solution for temporary databases

If the database file resides somewhere under /home (e.g. /home/Data/mydb.db or /home/site/wwwroot/mydb.db) the 'database is locked' error will occur.
I think this is due that all files under /home are persisted and reside in Azure Storage. Explained here

As solution use a different path. E.g. /tmp/mydb.db.
Or Path.Combine(Path.GetTempPath(), dbName);

Caveat: The db will be lost when the application restarts. So you can create the db in the temporary folder, then copy the db file to a persisted folder. Then access the db in the persisted folder with read only access. Add Mode=ReadOnly to the SQlite connection string (at least when using Microsoft.Data.Sqlite.SqliteConnection).

I fear there is no better solution when requiring Linux as OS. Microsoft should change this behaviour.

How do I unlock a SQLite database?

In windows you can try this program http://www.nirsoft.net/utils/opened_files_view.html to find out the process is handling db file. Try closed that program for unlock database

In Linux and macOS you can do something similar, for example, if your locked file is development.db:

$ fuser development.db

This command will show what process is locking the file:

> development.db: 5430

Just kill the process...

kill -9 5430

...And your database will be unlocked.

OperationalError: database is locked

From django doc:

SQLite is meant to be a lightweight
database, and thus can't 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.

Python's 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 you're 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 you've 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

http://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errorsoption



Related Topics



Leave a reply



Submit