0x80131904
dotnet
resource_error
ai_generated
true
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
85%Fix Rate
85%Confidence
1Evidence
2023-06-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| net6.0 | active | — | — | — |
| net7.0 | active | — | — | — |
| net8.0 | active | — | — | — |
| net9.0 | active | — | — | — |
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.
generic中文
连接池耗尽,因为连接未正确关闭/释放,或者池大小对于并发负载来说太小。
Official Documentation
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-poolingWorkarounds
-
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)
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) -
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.
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.
-
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.
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.
中文步骤
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)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.
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
Common approaches that don't work:
-
Increasing Max Pool Size in connection string to a very high value (e.g., 1000)
85% fail
This only masks the underlying issue of connection leaks; the pool will eventually exhaust again under higher load.
-
Restarting the application or IIS pool to clear connections
90% fail
Temporary fix; connections will leak again once the app runs, as the root cause (missing Dispose) persists.
-
Disabling connection pooling entirely (Pooling=false)
95% fail
Severely impacts performance by creating a new connection for every request, leading to slower response times and potential socket exhaustion.