# Microsoft.Data.SqlClient.SqlException (0x80131904): Connection 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`
- **Domain:** dotnet
- **Category:** resource_error
- **Error Code:** `0x80131904`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The connection pool is exhausted because connections are not being closed/disposed properly, or the pool size is too small for the concurrent load.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| net6.0 | active | — | — |
| net7.0 | active | — | — |
| net8.0 | active | — | — |
| net9.0 | active | — | — |

## Workarounds

1. **Ensure all DbContext instances are properly disposed by using 'using' blocks or dependency injection with AddDbContext and scoped lifetime. Example: 'using (var context = new MyDbContext()) { ... }' or services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Scoped)** (90% success)
   ```
   Ensure all DbContext instances are properly disposed by using 'using' blocks or dependency injection with AddDbContext and scoped lifetime. Example: 'using (var context = new MyDbContext()) { ... }' or services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Scoped)
   ```
2. **Increase connection pool size and add connection leak detection. Set 'Max Pool Size=200; Connection Lifetime=300' in the connection string, and enable 'Pooling=True' with 'Load Balance Timeout=30'. Also monitor with 'SELECT * FROM sys.dm_exec_sessions' to identify orphaned connections.** (80% success)
   ```
   Increase connection pool size and add connection leak detection. Set 'Max Pool Size=200; Connection Lifetime=300' in the connection string, and enable 'Pooling=True' with 'Load Balance Timeout=30'. Also monitor with 'SELECT * FROM sys.dm_exec_sessions' to identify orphaned connections.
   ```
3. **Use 'using var connection = new SqlConnection(connectionString);' and wrap in try-finally or use 'await using' for async operations. For EF Core, ensure 'context.Database.CloseConnection()' is called in long-running operations.** (85% success)
   ```
   Use 'using var connection = new SqlConnection(connectionString);' and wrap in try-finally or use 'await using' for async operations. For EF Core, ensure 'context.Database.CloseConnection()' is called in long-running operations.
   ```

## Dead Ends

- **Increasing Max Pool Size in connection string to a very high value (e.g., 1000)** — This only masks the underlying issue of connection leaks; the pool will eventually exhaust again under higher load. (85% fail)
- **Restarting the application or IIS pool to clear connections** — Temporary fix; connections will leak again once the app runs, as the root cause (missing Dispose) persists. (90% fail)
- **Disabling connection pooling entirely (Pooling=false)** — Severely impacts performance by creating a new connection for every request, leading to slower response times and potential socket exhaustion. (95% fail)
