Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Avoid Database Lock in SQLite3

  1. According to https://www.sqlite.org/lockingv3.html, POSIX advisory locking is known to be buggy or even unimplemented on many NFS implementations (including recent versions of Mac OS X) and that there are reports of locking problems for network filesystems under Windows. So, the rule of thumb is to avoid using SQLite3 on network filesystems (Samba, NFS, etc). You are guaranteed to encounter issues in the long run (even though there lots articles talking about ways to alleviate the problem). Use JSON (on NFS) if you don't really need database or use MySQL, etc. if a database is needed.

  2. Use the autocommit mode by using the option isolation_level=None when you use the sqlite3 module in Python. Notice that even if SQLite3 uses autocommit by default, the Python module sqlite3 does not have autocommit turned on by default.

Ways to Fix The Error "OperationError: Database is locked"

The best practice is to create a backup of the datase which has no locks on it. After that, replace the database with its backup copy.

Sqlite> .backup main backup.Sqlite
Sqlite> .exit
$mv .x.Sqlite old.Sqlite
$mv backup.Sqlite .x.Sqlite

You can also directly make a copy of the original SQLite3 file to backup it.

References

https://www.sqlite.org/lockingv3.html

https://www.arysontechnologies.com/blog/fix-sqlite-error-database-locked/

Can SQLite and TDB databases be used with NFS?

How do I unlock a SQLite database?

“The database file is locked” error even when the file is newly created

https://docs.python.org/3.8/library/sqlite3.html#sqlite3.connect

OperationalError: database is locked

How to know which process is responsible for a “OperationalError: database is locked”?

Comments