Write Ahead Logging (WAL) mode is a mode that enables more concurrent writes in SQLite. I implemented this as I have a local Django app that uses SQLite, and sometimes I would get database locks when to many processes were writing at once.
Here’s how to set it up in Django:
1. Create a new file (e.g., db_signals.py
) in one of your apps:
2. Add the following code to set the PRAGMA settings:
from django.db.backends.signals import connection_created
from django.dispatch import receiver
@receiver(connection_created)
def setup_sqlite_pragmas(sender, connection, **kwargs):
if connection.vendor == 'sqlite':
cursor = connection.cursor()
cursor.execute('PRAGMA journal_mode=wal;')
cursor.execute('PRAGMA busy_timeout=5000;')
cursor.close()
3. Import this file somewhere in your application to ensure the signal is connected, such as in the ready() method of your app’s AppConfig:
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'my_app'
def ready(self):
import my_app.db_signals
By setting up your database this way, you ensure that these PRAGMA settings are applied every time a new connection to the SQLite database is created, so you won’t need to manually set them each time.
Enjoy!