Skip to content
Latchkey

Elixir "** (DBConnection.ConnectionError)" in CI

DBConnection manages Ecto's connection pool. A ConnectionError means it could not hand out a working connection: the checkout timed out, the socket closed, or the database stopped accepting connections mid-run.

What this error means

Tests fail intermittently or in bulk with "** (DBConnection.ConnectionError)" mentioning "connection not available", "client timed out", or "tcp recv: closed".

** (DBConnection.ConnectionError)
** (DBConnection.ConnectionError) connection not available and request was dropped
from queue after 2950ms. This means requests are coming in and your connection
pool cannot serve them fast enough

Common causes

Pool exhausted or checkout timeout

More concurrent DB work than pool_size, or slow queries, empties the pool so requests time out waiting for a connection.

The database dropped connections

A restarting service container, an OOM-killed Postgres, or a network blip closes sockets, surfacing as "closed" ConnectionErrors.

How to fix it

Right-size the pool and timeouts for tests

  1. Set a pool_size that matches test concurrency for the test repo.
  2. Raise ownership/queue timeouts if checkouts are legitimately slow.
  3. Re-run and confirm connections are served.
config/test.exs
config :my_app, MyApp.Repo,
  pool: Ecto.Adapters.SQL.Sandbox,
  pool_size: System.schedulers_online() * 2,
  ownership_timeout: 60_000

Stabilize the database service

Ensure the Postgres service is health-checked and adequately resourced so it does not restart under load.

.github/workflows/ci.yml
options: >-
  --health-cmd pg_isready --health-interval 5s
  --health-timeout 5s --health-retries 10

How to prevent it

  • Tune pool_size to test concurrency and query cost.
  • Health-check and resource the DB service so it stays up.
  • Use the Ecto SQL Sandbox for isolated, connection-scoped tests.

Frequently asked questions

What causes ""** (DBConnection.ConnectionError)""?
More concurrent DB work than pool_size, or slow queries, empties the pool so requests time out waiting for a connection.
How do I fix "** (DBConnection.ConnectionError)"?
Right-size the pool and timeouts for tests

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card