semaphore-for-db-write-lock: sqlite only

See #117
This commit is contained in:
Klaas van Schelven
2025-07-25 15:22:35 +02:00
parent ff7db03643
commit b993112558

View File

@@ -7,6 +7,7 @@ import threading
from django.db import transaction as django_db_transaction
from django.db import DEFAULT_DB_ALIAS
from django.conf import settings
from snappea.settings import get_settings as get_snappea_settings
@@ -218,6 +219,17 @@ def immediate_atomic(using=None, savepoint=True, durable=True):
# case.
with immediate_atomic:
yield
elif "sqlite" not in settings.DATABASES[using]["ENGINE"]:
# The SemaphoreContext was added specifically to address the WAL growth issue in sqlite; better not to use it
# for other database backends; in particular, if such databases have longer default timeouts, then the error
# message may be confusing (semaphore timeout after 10s throws an error... while the thread that hogs the DB
# is _not_ (yet) timed out)
#
# in-string matching matches both our 'timed' backend and the django default.
with immediate_atomic:
yield
else:
# https://stackoverflow.com/a/45681273/339144 provides some context on nesting context managers; and how to
# proceed if you want to do this with an arbitrary number of context managers.