Skip to content

Fix #34: Database connection pool not configured with connection limits or error handling - #37

Merged
ai-mountain merged 2 commits into
ai-mountain:mainfrom
nandanadileep:fix-issue-34
Jun 17, 2026
Merged

Fix #34: Database connection pool not configured with connection limits or error handling#37
ai-mountain merged 2 commits into
ai-mountain:mainfrom
nandanadileep:fix-issue-34

Conversation

@nandanadileep

@nandanadileep nandanadileep commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #34 by configuring the PostgreSQL connection pool with production-ready limits, timeouts, and error handling, and by adding graceful shutdown logic.

Problem

backend/src/database/client.ts created a Pool with only connectionString. Under load this can lead to:

  • Connection exhaustion (default pool size may be too small)
  • Idle connections not being cleaned up
  • Unhandled database errors crashing the Node.js process
  • Connections left open during deploys / SIGTERM shutdowns

Changes

  • backend/src/database/client.ts

    • Added configurable pool limits:
      • DB_POOL_MAX (default 20)
      • DB_POOL_MIN (default 2)
      • DB_IDLE_TIMEOUT (default 30000 ms)
      • DB_CONNECTION_TIMEOUT (default 5000 ms)
    • Set allowExitOnIdle: false to keep the process alive while the pool is running
    • Added a pool.on('error', …) handler so idle-client errors are logged instead of crashing the process
    • Exported a new closePool() helper for clean shutdown
  • backend/src/server.ts

    • Imported the database pool
    • Stored the server instance returned by app.listen
    • Added a SIGTERM handler that:
      1. Closes the database pool
      2. Closes the HTTP server
      3. Exits cleanly

Environment variables

No breaking changes. Existing deployments keep working because every new setting has a sensible default. Optional overrides:

Variable Default Description
DB_POOL_MAX 20 Maximum connections in the pool
DB_POOL_MIN 2 Minimum idle connections
DB_IDLE_TIMEOUT 30000 Milliseconds before an idle client is closed
DB_CONNECTION_TIMEOUT 5000 Milliseconds to wait when connecting

Testing

  • Server starts normally with DATABASE_URL set
  • Sending SIGTERM closes the pool and exits without open handles
  • Pool error events are logged and do not crash the process

Related

@ai-mountain

Copy link
Copy Markdown
Owner

@nandanadileep Thank you for your contributing.
But I hope u explain more details about description.

@ai-mountain

Copy link
Copy Markdown
Owner

@nandanadileep plz update Description before merging this PR.

@nandanadileep

Copy link
Copy Markdown
Contributor Author

@RenzoMXD Updated the PR description with the details.

@ai-mountain

Copy link
Copy Markdown
Owner

@nandanadileep please fix ci test failing.

@ai-mountain
ai-mountain merged commit a6eecf7 into ai-mountain:main Jun 17, 2026
2 checks passed
@ai-mountain

Copy link
Copy Markdown
Owner

Thanks for your contribution. @nandanadileep

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.

Database connection pool not configured with connection limits or error handling

2 participants