Tutorials Entity Framework Core Tutorial
Connection Pooling in EF Core
Connection Pooling in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.
On this page
Entity Framework Core Tutorial · Lesson 67 of 100
Connection Pooling in EF Core
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~10 min · Module 7: Performance Optimization
What is this?
ADO.NET connection pooling reuses open SQL connections behind EF Core. Pooling is automatic — problems arise from connection leaks or exhausting pool size.
Why should you care?
ShopNest traffic spikes fail with "timeout waiting for connection" if connections are held open too long or pool starved.
See it live — copy this example
Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).
// Good: short-lived context per request (default Scoped)
// Bad: manual connection left open
await using var conn = new SqlConnection(connectionString);
await conn.OpenAsync();
// always dispose — EF manages via DbContext dispose
services.AddDbContextPool<ShopNestDbContext>(options =>
options.UseSqlServer(connectionString), poolSize: 128);
What happened?
- DbContextPool reuses context instances with reset state — reduces allocation.
- Underlying SqlConnection still pools at ADO.NET layer when contexts dispose promptly.
Practice next
- Keep DbContext Scoped per request — do not hold across await external APIs long.
- Consider AddDbContextPool for high-throughput read APIs.
- Monitor SQL Server connection count during load tests.
- Load test with vs without AddDbContextPool measuring allocations.
- Log pool exhaustion errors and correlate with slow external calls inside transactions.
Remember
SQL connections pool automatically when disposed. DbContextPool reduces context allocation overhead. Avoid long-lived contexts and leaks.
ShopNest pool exhaustion fix
Checkout held transaction during 30s payment SDK call — pool exhausted; team moved payment outside transaction.
Outcome: Connection timeouts disappear under peak load.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!