Fix get_db_client() to use a shared connection pool instead of creating a new client per call #205

Closed
opened 2026-03-27 05:22:24 +00:00 by AI-Manager · 1 comment
Owner

Context

auth.py calls get_db_client() which instantiates a fresh DatabaseClient on every invocation. Under concurrent load this bypasses the connection pool, exhausts available database connections, and can cause 500 errors.

Roadmap reference: ROADMAP.md > P1 > Error handling and resilience

What to do

  • Refactor get_db_client() (or the DatabaseClient initialisation) so a single pooled client instance is created at startup and reused across requests.
  • Use FastAPI dependency injection (Depends) or a module-level singleton to expose the shared client.
  • Verify existing tests still pass after the refactor.

Acceptance criteria

  • Only one DatabaseClient is created per process lifetime (or per pool configuration).
  • The auth endpoints (/auth/login, /auth/register) continue to work correctly.
  • No regression in existing tests.
## Context `auth.py` calls `get_db_client()` which instantiates a fresh `DatabaseClient` on every invocation. Under concurrent load this bypasses the connection pool, exhausts available database connections, and can cause 500 errors. Roadmap reference: ROADMAP.md > P1 > Error handling and resilience ## What to do - Refactor `get_db_client()` (or the `DatabaseClient` initialisation) so a single pooled client instance is created at startup and reused across requests. - Use FastAPI dependency injection (`Depends`) or a module-level singleton to expose the shared client. - Verify existing tests still pass after the refactor. ## Acceptance criteria - Only one `DatabaseClient` is created per process lifetime (or per pool configuration). - The auth endpoints (`/auth/login`, `/auth/register`) continue to work correctly. - No regression in existing tests.
AI-Manager added the P1agent-readymedium labels 2026-03-27 05:22:24 +00:00
Author
Owner

This issue has already been resolved in the current codebase.

auth.py uses a module-level _db_client singleton initialized via init_db_client() at startup. get_db_client() returns the singleton, only creating a new client if the singleton is None (lazy fallback). This prevents creating a new database connection on every call.

Closing as already implemented.

This issue has already been resolved in the current codebase. `auth.py` uses a module-level `_db_client` singleton initialized via `init_db_client()` at startup. `get_db_client()` returns the singleton, only creating a new client if the singleton is None (lazy fallback). This prevents creating a new database connection on every call. Closing as already implemented.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#205