149๐
From Djangoโs documentation:
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 thansqlite
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 theOperationalError: 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:"OPTIONS": { # ... "timeout": 20, # ... }
This will make SQLite wait a bit longer before throwing "database is locked"
errors; it wonโt really do anything to solve them.
80๐
In my case, It was because I open the database from SQLite Browser. When I close it from the browser, the problem is gone.
- [Django]-Choose test database?
- [Django]-Chained method calls indentation style in Python
- [Django]-Getting the SQL from a Django QuerySet
66๐
I slightly disagree with the accepted answer which, by quoting this doc, implicitly links OPโs problem (Database is locked
) to this:
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.
This is a bit "too easy" to incriminate SQlite for this problem (which is very powerful when correctly used; itโs not only a toy for small databases, fun fact: An SQLite database is limited in size to 140 terabytes
).
Unless you have a very busy server with thousands of connections at the same second, the reason for this Database is locked
error is probably more a bad use of the API, than a problem inherent to SQlite which would be "too light". Here are more informations about Implementation Limits for SQLite.
Now the solution:
I had the same problem when I was using two scripts using the same database at the same time:
- one was accessing the DB with write operations
- the other was accessing the DB in read-only
Solution: always do cursor.close()
as soon as possible after having done a (even read-only) query.
- [Django]-Best practice for Django project working directory structure
- [Django]-How to serve media files on Django production environment?
- [Django]-Django manage.py runserver invalid syntax
49๐
The practical reason for this is often that the python or django shells have opened a request to the DB and it wasnโt 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 youโd like to kill access without rebooting the terminal, then from commandline you can do:
from django import db
db.connections.close_all()
- [Django]-Django TextField and CharField is stripping spaces and blank lines
- [Django]-Django Rest Framework remove csrf
- [Django]-Django: show the count of related objects in admin list_display
10๐
As others have told, there is another process that is using the SQLite file and has not closed the connection. In case you are using Linux, you can see which processes are using the file (for example db.sqlite3
) using the fuser
command as follows:
$ sudo fuser -v db.sqlite3
USER PID ACCESS COMMAND
/path/to/db.sqlite3:
user 955 F.... apache2
If you want to stop the processes to release the lock, use fuser -k
which sends the KILL
signal to all processes accessing the file:
sudo fuser -k db.sqlite3
Note that this is dangerous as it might stop the web server process in a production server.
Thanks to @cz-game for pointing out fuser
!
- [Django]-Django โ how to unit test a post request using request.FILES
- [Django]-Bypass confirmation prompt for pip uninstall
- [Django]-Unique fields that allow nulls in Django
7๐
I got this error when using a database file saved under WSL (\\wsl$ โฆ) and running a windows python interpreter.
You can either not save the database in your WSL-tree or use a linux based interpreter in your distro.
- [Django]-IOS app with Django
- [Django]-<Django object > is not JSON serializable
- [Django]-How to revert the last migration?
6๐
I encountered this error message in a situation that is not (clearly) addressed by the help info linked in patrickโs answer.
When I used transaction.atomic()
to wrap a call to FooModel.objects.get_or_create()
and called that code simultaneously from two different threads, only one thread would succeed, while the other would get the โdatabase is lockedโ error. Changing the timeout database option had no effect on the behavior.
I think this is due to the fact that sqlite cannot handle multiple simultaneous writers, so the application must serialize writes on their own.
I solved the problem by using a threading.RLock
object instead of transaction.atomic()
when my Django app is running with a sqlite backend. Thatโs not entirely equivalent, so you may need to do something else in your application.
Hereโs my code that runs FooModel.objects.get_or_create
simultaneously from two different threads, in case it is helpful:
from concurrent.futures import ThreadPoolExecutor
import configurations
configurations.setup()
from django.db import transaction
from submissions.models import ExerciseCollectionSubmission
def makeSubmission(user_id):
try:
with transaction.atomic():
e, _ = ExerciseCollectionSubmission.objects.get_or_create(
student_id=user_id, exercise_collection_id=172)
except Exception as e:
return f'failed: {e}'
e.delete()
return 'success'
futures = []
with ThreadPoolExecutor(max_workers=2) as executor:
futures.append(executor.submit(makeSubmission, 296))
futures.append(executor.submit(makeSubmission, 297))
for future in futures:
print(future.result())
- [Django]-How to disable Django's CSRF validation?
- [Django]-Django connection to postgres by docker-compose
- [Django]-Malformed Packet: Django admin nested form can't submit, connection was reset
6๐
I was facing this issue in my flask app because I opened the database in SQLite Browser and forgot to write the changes.
If you have also made any changes in SQLite Browser, then click on write changes and everything will be fine.
- [Django]-How does django handle multiple memcached servers?
- [Django]-ImportError: Failed to import test module:
- [Django]-Timestamp fields in django
3๐
For me it gets resolved once I closed the django shell which was opened using python manage.py shell
- [Django]-How to understand lazy function in Django utils functional module
- [Django]-Django โ how to create a file and save it to a model's FileField?
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
2๐
This also could happen if you are connected to your sqlite db via dbbrowser plugin through pycharm. Disconnection will solve the problem
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2
- [Django]-No module named MySQLdb
2๐
Iโve got the same error! One of the reasons was the DB connection was not closed.
Therefore, check for unclosed DB connections. Also, check if you have committed the DB before closing the connection.
- [Django]-What does on_delete do on Django models?
- [Django]-Macros in django templates
- [Django]-Django โ How to rename a model field using South?
2๐
I had a similar error, right after the first instantiation of Django (v3.0.3). All recommendations here did not work apart from:
- deleted the
db.sqlite3
file and lose the data there, if any, python manage.py makemigrations
python manage.py migrate
Btw, if you want to just test PostgreSQL:
docker run --rm --name django-postgres \
-e POSTGRES_PASSWORD=mypassword \
-e PGPORT=5432 \
-e POSTGRES_DB=myproject \
-p 5432:5432 \
postgres:9.6.17-alpine
Change the settings.py
to add this DATABASES
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'postgres',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
โฆand add database adapter:
pip install psycopg2-binary
Then the usual:
python manage.py makemigrations
python manage.py migrate
- [Django]-Numeric for loop in Django templates
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-How to perform OR condition in django queryset?
2๐
Check if your database is opened on another DB Browser.
If it is opened on an other application, then close the application and run the program again.
- [Django]-Define css class in django Forms
- [Django]-ModuleNotFoundError: No module named 'grp' on windows
- [Django]-Django rest framework change primary key to use a unqiue field
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-Problems extend change_form.html in django admin
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
1๐
I found this worked for my needs. (thread locking) YMMV
conn = sqlite3.connect(database, timeout=10)
https://docs.python.org/3/library/sqlite3.html
sqlite3.connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])
When a database is accessed by multiple connections, and one of the processes modifies the database, the SQLite database is locked until that transaction is committed. The timeout parameter specifies how long the connection should wait for the lock to go away until raising an exception. The default for the timeout parameter is 5.0 (five seconds).
- [Django]-Django: Using F arguments in datetime.timedelta inside a query
- [Django]-Multiple annotate Sum terms yields inflated answer
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
1๐
In my case, I added a new record manually saved and again through shell tried to add new record this time it works perfectly check it out.
In [7]: from main.models import Flight
In [8]: f = Flight(origin="Florida", destination="Alaska", duration=10)
In [9]: f.save()
In [10]: Flight.objects.all()
Out[10]: <QuerySet [<Flight: Flight object (1)>, <Flight: Flight object (2)>, <Flight: Flight object (3)>, <Flight: Flight object (4)>]>
- [Django]-How to mix queryset results?
- [Django]-Django logging of custom management commands
- [Django]-How to manually assign imagefield in Django
1๐
actually I have faced same problem , when I use "transaction.atomic() with select_for_update() " i got error message "the OperationalError: database is locked" ,
and after many tries / searching / read django docs ,
i found the problem from SQLite itself it is not support select_for_update method as django DOCs says , kindly have a look at the following url and read it deeply:
https://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errors
, and when i moved to MySQL everything goes fine .
as django DOCs also says "database is locked" may happen when database timeout occur ,
they recommend you to change database timeout by setting up the following option :
'OPTIONS': {
# ...
'timeout': 20,
# ...
}
finally, I recommend you to use MySQL/PostgreSQL even if you working on development environment .
I hope this helpful for you .
- [Django]-Using JSON in django template
- [Django]-What is a "slug" in Django?
- [Django]-How to change empty_label for modelForm choice field?
0๐
In my case, I had not saved a database operation I performed within the SQLite Browser. Saving it solved the issue.
- [Django]-Can I access constants in settings.py from templates in Django?
- [Django]-Django โ How to use decorator in class-based view methods?
- [Django]-Printing Objects in Django
0๐
A very unusual scenario, which happened to me.
There was infinite recursion, which kept creating the objects.
More specifically, using DRF, I was overriding create method in a view, and I did
def create(self, request, *args, **kwargs):
....
....
return self.create(request, *args, **kwargs)
- [Django]-Is it better to use path() or url() in urls.py for django 2.0?
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-Remove pk field from django serialized objects
0๐
Already lot of Answers are available here, even I want to share my case , this may help someone..
I have opened the connection in Python API to update values, Iโll close connection only after receiving server response. Here what I did was I have opened connection to do some other operation in server as well before closing the connection in Python API.
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-Django self-referential foreign key
- [Django]-Separating form input and model validation in Django?
0๐
If you get this error while using manage.py shell
, one possible reason is that you have a development server running (manage.py runserver
) which is locking the database. Stoping the server while using the shell has always fixed the problem for me.
- [Django]-Form with CheckboxSelectMultiple doesn't validate
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-How do I create a slug in Django?
0๐
I got this error when attempting to create a new table in SQLite but the session
object contained uncommitted (though flushed) changes.
Make sure to either:
- Commit the session(s) before creating a new table
- Close all sessions and perform the table creation in a new connection
- โฆ
- [Django]-Django TemplateSyntaxError โ 'staticfiles' is not a registered tag library
- [Django]-How to upload a file in Django?
- [Django]-How to get the domain name of my site within a Django template?
0๐
@Shilp Thapakโs answer is correct: the reason for the error is that you did not write your manual changes to the data in your DB Browser for SQLite before running your application.
If you didnโt write the changes in whatever SQL client you are using, you can still create the engine but
engine.connect()
will throw the operational error about the database being locked.
You can check whether your engine can connect by checking the existence of a rollback journal. The default mode of a rollback journal is to be created and deleted at the start and end of a transaction.
It is exists in the same directory where your database is, it has the same name as the database file and the suffix "-journal" appended.
If the mode is not changed, at Journal mode in Edit pragmas panel in DB Browser for SQLite.
You can check the existence of the temp file like so:
if os.path.isfile('your-database.sqlite-journal'):
print("The database is locked. Please write your changes in your SQL client before proceeding.\n")
Read more about temporary files here.
So no need to close the server or DB Browser for SQLite for that sake. In fact, as long as all the changes are written, you can have several clients connected to the database simultaneously and still run your application at the same time.
- [Django]-Add rich text format functionality to django TextField
- [Django]-Django admin and MongoDB, possible at all?
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
0๐
if youre using more than one development editors working on that poject, it may cause the same error.
for example, spyder and vscode running the same โprj.pyโ file, creates a conflict in some processes, like deleting records.
- [Django]-Combining Django F, Value and a dict to annotate a queryset
- [Django]-In Django, how does one filter a QuerySet with dynamic field lookups?
- [Django]-Celery : Execute task after a specific time gap
-1๐
For me it was simply because I was accessing the database in SQLite app at the same time of running my Python code to create a new table.
Closing SQLite until the code is done solved my issue.
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-Django: How to format a DateField's date representation?
- [Django]-Django model "doesn't declare an explicit app_label"
-2๐
UPDATE django version 2.1.7
I got this error sqlite3.OperationalError: database is locked
using pytest
with django
.
Solution:
If we are using @pytest.mark.django_db
decorator. What it does is create a in-memory-db
for testing.
Named: file:memorydb_default?mode=memory&cache=shared
We can get this name with:
from django.db import connection
db_path = connection.settings_dict['NAME']
To access this database and also edit it, do:
Connect to the data base:
with sqlite3.connect(db_path, uri=True) as conn:
c = conn.cursor()
Use uri=True
to specifies the disk file that is the SQLite database to be opened.
To avoid the error activate transactions in the decorator:
@pytest.mark.django_db(transaction=True)
Final function:
from django.db import connection
@pytest.mark.django_db(transaction=True)
def test_mytest():
db_path = connection.settings_dict['NAME']
with sqlite3.connect(db_path, uri=True) as conn:
c = conn.cursor()
c.execute('my amazing query')
conn.commit()
assert ... == ....
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
- [Django]-What is related_name used for?
- [Django]-Error: "dictionary update sequence element #0 has length 1; 2 is required" on Django 1.4
-2๐
Just reboot your server, it will clear all current processes that have your database locked.
- [Django]-Validators = [MinValueValidator] does not work in Django
- [Django]-Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events
- [Django]-How to format time in django-rest-framework's serializer?
-2๐
I just needed to add alias sqlite='sqlite3'
to my ~/.zshrc
I then deleted the partially-failed creation of the virtualenv
in ~/.pyenv/versions/new-virtualenv
and reran pyenv virtualenv <name>
and it worked swimmingly
- [Django]-Django return file over HttpResponse โ file is not served correctly
- [Django]-How can I create a deep clone of a DB object in Django?
- [Django]-Separating form input and model validation in Django?
-2๐
you can use
"sudo fuser -v db.sqlite3"
to determine the processes (python) accessing the db.sqlite3 then use
"sudo kill -9 PID1 PID2"
to terminate those processes and PID1 is the id of the first process and PID2 is the id of the second process etcโฆ
- [Django]-Problems extend change_form.html in django admin
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-What's the best way to store a phone number in Django models?
- [Django]-Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests?
- [Django]-Django {% if forloop.first %} question
- [Django]-Why does DEBUG=False setting make my django Static Files Access fail?