Devr.AI icon indicating copy to clipboard operation
Devr.AI copied to clipboard

Fix: Thread-safe DB connection manager with pooling (#212)

Open muhammadtihame opened this issue 10 months ago β€’ 3 comments

Summary

This PR implements thread-safe database connection management with connection pooling and proper resource cleanup.

Changes

  • Added backend/app/database/core.py (async SQLAlchemy engine + session manager)
  • Added tests/test_db_pool.py to verify concurrency and rollback behavior
  • Added docs/DATABASE_CONNECTION.md documentation
  • Updated backend/app/core/config/settings.py for database configuration
  • Updated pyproject.toml and backend/requirements.txt to include new dependencies

Verification

Run the following tests:

pytest tests/test_db_pool.py


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **New Features**
  * Added optional environment-configurable database URL and asynchronous database connectivity with pooled connections for improved performance and reliability.

* **Documentation**
  * Added a guide covering database connection setup, pooling behavior, and best practices.

* **Tests**
  * Added tests for connection pool sizing, concurrent session handling, and rollback/cleanup on errors.

* **Chores**
  * Added async DB runtime and test dependencies.

<sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

muhammadtihame avatar Jan 26 '26 15:01 muhammadtihame

πŸ“ Walkthrough

Walkthrough

Adds async database support via SQLAlchemy/asyncpg: new optional database_url setting, an async engine/session module with pooling and a get_db() generator, updated dependencies, documentation, and tests for pool behavior and session lifecycle.

Changes

Cohort / File(s) Summary
Configuration
backend/app/core/config/settings.py
Added optional database_url: Optional[str] = None to Settings.
Async DB core
backend/app/database/core.py
New async engine (create_async_engine) and async_session_maker created from DATABASE_URL; engine may be None with a warning; added async def get_db() -> AsyncGenerator[AsyncSession, None] that raises if engine uninitialized, yields sessions, and handles rollback/closure on errors.
Project deps
pyproject.toml, backend/requirements.txt
Added sqlalchemy and asyncpg to project dependencies; added asyncpg==0.29.0 and pytest-asyncio==0.23.5 to backend requirements.
Documentation
docs/DATABASE_CONNECTION.md
New doc describing AsyncIO SQLAlchemy setup, pool parameters, DATABASE_URL usage, get_db DI pattern, and testing considerations.
Tests
tests/test_db_pool.py
New async tests mocking engine/session: asserts pool size/timeout, simulates 50 concurrent session acquisitions, and verifies rollback+close on error.

Sequence Diagram(s)

sequenceDiagram
    participant Client as FastAPI Route
    participant GetDb as get_db()
    participant Maker as async_session_maker
    participant Pool as Connection Pool
    participant Session as AsyncSession

    Client->>GetDb: request session
    alt Engine initialized
        GetDb->>Maker: create session
        Maker->>Pool: acquire connection
        Pool-->>Maker: provide connection
        Maker-->>Session: return AsyncSession
        GetDb-->>Client: yield session
        Client->>Session: execute queries
        alt Success
            Client->>GetDb: return normally
            GetDb->>Session: close session
            Session->>Pool: release connection
        else Exception
            Client->>GetDb: exception raised
            GetDb->>Session: rollback
            GetDb->>Session: close session
            Session->>Pool: release connection
        end
    else Engine not initialized
        GetDb-->>Client: raise RuntimeError
    end

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

πŸ‡ I nibble bytes and hop through queues,

Connections pooled, a cozy fuse.
Sessions yield and roll back light,
Async fields gleam in rabbit sight.
Hooray β€” the DB sleeps snug at night!

πŸš₯ Pre-merge checks | βœ… 3
βœ… Passed checks (3 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title 'Fix: Thread-safe DB connection manager with pooling (#212)' directly and accurately describes the main change: implementing a thread-safe database connection manager with connection pooling, which is the primary objective of the PR across all modified files.
Docstring Coverage βœ… Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • [ ] πŸ“ Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jan 26 '26 15:01 coderabbitai[bot]

Fixes #212

muhammadtihame avatar Jan 26 '26 15:01 muhammadtihame

Addressed CodeRabbit feedback:

  • Removed redundant session.close()
  • Switched to logger.exception for better tracing
  • Refactored tests to use importlib.reload with fixture
  • Fixed async mock aexit using AsyncMock

muhammadtihame avatar Jan 26 '26 15:01 muhammadtihame