{
  "id": "python/sqlalchemy-session-closed-connection",
  "signature": "sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly",
  "signature_zh": "sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 服务器意外关闭了连接",
  "regex": "sqlalchemy\\.exc\\.OperationalError:\\ \\(psycopg2\\.OperationalError\\)\\ server\\ closed\\ the\\ connection\\ unexpectedly",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The database connection was closed by the server, often due to a timeout or network issue, and the session is still using the stale connection.",
  "root_cause_type": "generic",
  "root_cause_zh": "数据库连接被服务器关闭，通常是由于超时或网络问题，而会话仍在使用的连接已失效。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Restarting the database server",
      "why_fails": "Restarting the server does not fix the underlying issue of connection pooling or timeout settings.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using session.commit() to retry",
      "why_fails": "Commit does not reset the connection; it only flushes pending changes.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Recreate the session using a fresh engine connection",
      "success_rate": 0.95,
      "how": "from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nengine = create_engine('postgresql://user:pass@localhost/db', pool_pre_ping=True)\nSession = sessionmaker(bind=engine)\nsession = Session()\n# Use session for queries",
      "condition": "",
      "sources": []
    },
    {
      "action": "Enable pool_pre_ping to check connection health",
      "success_rate": 0.9,
      "how": "engine = create_engine('postgresql://user:pass@localhost/db', pool_pre_ping=True)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}