# System.InvalidOperationException：超时已过期。在从池中获取连接之前，超时期限已过。这可能是因为所有池化连接都在使用中并且已达到最大池大小。

- **ID:** `cloud/azure-functions-dynamic-sku-sql-connection-pool-exhausted`
- **领域:** cloud
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

消耗（动态）计划中的 Azure Functions 在负载下快速扩展，创建许多并发实例，这些实例耗尽了 SQL 数据库连接池（默认最大池大小 = 100），因为每个实例都会打开新连接而无法有效重用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Azure Functions runtime v4 (.NET 8) | active | — | — |
| Azure SQL Database (DTU-based S2 tier) | active | — | — |
| System.Data.SqlClient 4.8.6 | active | — | — |

## 解决方案

1. ```
   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;`
   ```
2. ```
   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.
   ```

## 无效尝试

- **** — The issue is connection pooling, not the SQL query itself; optimizing a single query won't reduce the number of concurrent connections. (60% 失败率)
- **** — The function timeout is separate from the SQL connection timeout; increasing it does not affect pool exhaustion. (85% 失败率)
- **** — Scaling up to a higher SKU may help but is often unnecessary and costly; the root cause is connection management, not database capacity. (40% 失败率)
