# System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

- **ID:** `dotnet/ef-core-connection-timeout-pool-exhaustion`
- **Domain:** dotnet
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

Connection pool exhaustion occurs when database connections are not properly disposed, causing all pooled connections to remain open and new requests to wait indefinitely.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Entity Framework Core 7.0 | active | — | — |
| Entity Framework Core 8.0 | active | — | — |
| Microsoft.Data.SqlClient 5.1.0 | active | — | — |

## Workarounds

1. **Ensure all DbContext instances are properly disposed by using 'using' statements or dependency injection scoped lifetime. For example: 'using (var context = new MyDbContext()) { ... }' or register as scoped in DI: 'services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Scoped)'.** (85% success)
   ```
   Ensure all DbContext instances are properly disposed by using 'using' statements or dependency injection scoped lifetime. For example: 'using (var context = new MyDbContext()) { ... }' or register as scoped in DI: 'services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Scoped)'.
   ```
2. **If using async operations, ensure every async call is awaited to avoid connection leaks from incomplete tasks. Use 'await using' for asynchronous disposal: 'await using var context = new MyDbContext();'** (80% success)
   ```
   If using async operations, ensure every async call is awaited to avoid connection leaks from incomplete tasks. Use 'await using' for asynchronous disposal: 'await using var context = new MyDbContext();'
   ```
3. **Temporarily increase Max Pool Size and Connection Timeout in the connection string as a short-term mitigation while fixing the root cause: 'Server=myServer;Database=myDB;Max Pool Size=200;Connection Timeout=30;'** (70% success)
   ```
   Temporarily increase Max Pool Size and Connection Timeout in the connection string as a short-term mitigation while fixing the root cause: 'Server=myServer;Database=myDB;Max Pool Size=200;Connection Timeout=30;'
   ```

## Dead Ends

- **** — Increasing Max Pool Size in the connection string only delays the issue; it does not fix the underlying leak. (75% fail)
- **** — Restarting the application temporarily clears the pool but connections will leak again if disposal is not fixed. (90% fail)
- **** — Adding retry logic without fixing disposal causes the same timeout to repeat, wasting resources and slowing response. (80% fail)
