{
  "id": "dotnet/ef-core-connection-timeout-pool-exhaustion",
  "signature": "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.",
  "signature_zh": "System.InvalidOperationException: 超时已过期。在从池中获取连接之前超时时间已过。这可能是因为所有池化连接均在使用中且已达到最大池大小。",
  "regex": "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\\.",
  "domain": "dotnet",
  "category": "data_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "连接池耗尽发生在数据库连接未正确释放时，导致所有池化连接保持打开状态，新请求无限期等待。",
  "versions": [
    {
      "version": "Entity Framework Core 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Entity Framework Core 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Microsoft.Data.SqlClient 5.1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Increasing Max Pool Size in the connection string only delays the issue; it does not fix the underlying leak.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Restarting the application temporarily clears the pool but connections will leak again if disposal is not fixed.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Adding retry logic without fixing disposal causes the same timeout to repeat, wasting resources and slowing response.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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)'.",
      "success_rate": 0.85,
      "how": "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)'.",
      "condition": "",
      "sources": []
    },
    {
      "action": "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();'",
      "success_rate": 0.8,
      "how": "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();'",
      "condition": "",
      "sources": []
    },
    {
      "action": "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;'",
      "success_rate": 0.7,
      "how": "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;'",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "确保所有 DbContext 实例通过使用 'using' 语句或依赖注入作用域生命周期正确释放。例如：'using (var context = new MyDbContext()) { ... }' 或在 DI 中注册为作用域：'services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Scoped)'。",
    "如果使用异步操作，确保每个异步调用都被 await 以避免未完成任务导致连接泄漏。使用 'await using' 进行异步释放：'await using var context = new MyDbContext();'",
    "临时增加连接字符串中的 Max Pool Size 和 Connection Timeout 作为短期缓解措施，同时修复根本原因：'Server=myServer;Database=myDB;Max Pool Size=200;Connection Timeout=30;'"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-06-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}