Skip to content

fix(database): Fixed concurrency issues in DB queries between containers#583

Merged
jackseceng merged 2 commits into
mainfrom
db-issue-fixes-2
Jul 10, 2026
Merged

fix(database): Fixed concurrency issues in DB queries between containers#583
jackseceng merged 2 commits into
mainfrom
db-issue-fixes-2

Conversation

@jackseceng

Copy link
Copy Markdown
Owner

Replaced the per-request _create_connection() / conn.close() pattern with a module-level singleton _get_connection(). Each Gunicorn worker now opens one connection for its lifetime and pulls the remote replica once at startup, rather than reconnecting on every request. The replica path is now derived inside _get_connection() using getpid() after the worker fork, giving each worker its own file (e.g. urls-25.db
). This eliminates the DatabaseError: File is locked by another process errors that occurred when all 25 workers tried to open the same path simultaneously. Added conn.push() after commit() in insert_link and increment_click. Without this, writes only existed in the local replica and were lost on container restart since /tmp is ephemeral.

BREAKING CHANGE: Updating database logic

Replaced the per-request _create_connection() / conn.close() pattern with a module-level singleton _get_connection(). Each Gunicorn worker now opens one connection for its lifetime and pulls the remote replica once at startup, rather than reconnecting on every request.
The replica path is now derived inside _get_connection() using getpid() after the worker fork, giving each worker its own file (e.g.
urls-25.db
). This eliminates the DatabaseError: File is locked by another process errors that occurred when all 25 workers tried to open the same path simultaneously.
Added conn.push() after commit() in insert_link and increment_click. Without this, writes only existed in the local replica and were lost on container restart since /tmp is ephemeral.

BREAKING CHANGE: Updating database logic
@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

Super-linter summary

Language Validation result
CSS Pass ✅
DOCKERFILE_HADOLINT Pass ✅
GITHUB_ACTIONS Pass ✅
GITLEAKS Pass ✅
HTML Pass ✅
JAVASCRIPT_PRETTIER Pass ✅
MARKDOWN Pass ✅
PYTHON_BLACK Pass ✅
PYTHON_ISORT Pass ✅
YAML Pass ✅

All files and directories linted successfully

For more information, see the GitHub Actions workflow run

Powered by Super-linter

@jackseceng jackseceng marked this pull request as ready for review July 10, 2026 20:49
@jackseceng jackseceng merged commit b22fa68 into main Jul 10, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant