System.InvalidOperationException:超时已过期。在从池中获取连接之前,超时期限已过。这可能是因为所有池化连接都在使用中并且已达到最大池大小。
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: cloud/azure-functions-dynamic-sku-sql-connection-pool-exhausted
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Azure Functions runtime v4 (.NET 8) | active | — | — | — |
| Azure SQL Database (DTU-based S2 tier) | active | — | — | — |
| System.Data.SqlClient 4.8.6 | active | — | — | — |
根因分析
消耗(动态)计划中的 Azure Functions 在负载下快速扩展,创建许多并发实例,这些实例耗尽了 SQL 数据库连接池(默认最大池大小 = 100),因为每个实例都会打开新连接而无法有效重用。
English
Azure Functions on the Consumption (dynamic) plan scale out rapidly under load, creating many concurrent instances that exhaust the SQL Database connection pool (default max pool size = 100) because each instance opens new connections without reusing them efficiently.
官方文档
https://docs.microsoft.com/en-us/azure/azure-functions/manage-connections解决方案
-
Increase the Max Pool Size in the connection string to accommodate more concurrent connections (e.g., add 'Max Pool Size=200'): `Server=tcp:myserver.database.windows.net;Database=mydb;User Id=myuser;Password=mypwd;Max Pool Size=200;`
-
Implement connection multiplexing by using a singleton HttpClient or Dapper with a shared DbConnection in the Azure Functions startup class to reuse connections across invocations.
无效尝试
常见但无效的做法:
-
60% 失败
The issue is connection pooling, not the SQL query itself; optimizing a single query won't reduce the number of concurrent connections.
-
85% 失败
The function timeout is separate from the SQL connection timeout; increasing it does not affect pool exhaustion.
-
40% 失败
Scaling up to a higher SKU may help but is often unnecessary and costly; the root cause is connection management, not database capacity.