0Pricing
MCP Academy · Lesson

Pool Connections in Lifespan

Reuse DB and HTTP clients across requests.

Pool Connections in Lifespan is a free MCP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Connecting Costs Time

Opening a fresh database or HTTP connection on every call is slow. The handshake adds latency that piles up fast under load. 🐢

Reuse with a Pool

A connection pool keeps a set of live connections ready, lending one per request and taking it back when the work is done.

Build the Pool at Startup

The lifespan hook is the perfect home: create the pool once when the server boots, before any tool runs.

@asynccontextmanager
async def lifespan(server):
    pool = await create_pool(DSN)
    yield {"pool": pool}

Yield It as Shared State

Whatever you yield from the lifespan becomes shared context, so every handler reaches the same pool without rebuilding it.

Reach It via Context

Inside a tool, grab the pool from the request context rather than opening your own connection each time.

pool = ctx.request_context.lifespan_context["pool"]

Borrow, Use, Return

Acquire a connection from the pool, run your query, and let it return automatically. The with block handles checkout and release.

async with pool.acquire() as conn:
    rows = await conn.fetch(sql)

Cap the Pool Size

Set a max size so the pool never opens more connections than your database can handle, even when calls flood in.

pool = await create_pool(DSN, max_size=10)

Share One HTTP Client Too

The same idea fits HTTP: build one httpx client in the lifespan and reuse it so connections stay warm and pooled.

client = httpx.AsyncClient()
yield {"http": client}

Pools Are Thread-Safe

A good async pool is built for concurrency, safely handing connections to many requests running at the same time.

Watch for Leaks

Always release what you borrow. A connection you forget to return is a leak that slowly starves the pool until calls stall.

Close It on Shutdown

After the lifespan yield, close the pool so connections drain cleanly when the server stops.

    yield {"pool": pool}
    await pool.close()

Quick Check

Where do you build a connection pool?

Recap: Pooling

You pooled connections in the lifespan: build once, share via context, borrow per call, and close on shutdown. Next, cache and rate-limit. 🎯

Frequently asked questions

Is the “Pool Connections in Lifespan” lesson free?

Yes — the full text of “Pool Connections in Lifespan” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Pool Connections in Lifespan”?

Reuse DB and HTTP clients across requests. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pool Connections in Lifespan” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Expose SQL Queries Safely
  2. Wrap a REST API as Tools
  3. Pool Connections in Lifespan
  4. Cache & Rate-Limit Upstreams
← Back to MCP Academy