Skip to content

连接 Timeout

错误信息

                timeout expired 虽然 waiting for 连接
              

快速摘要

连接 pool 有否 available 连接 and 你的请求 waited longer than 配置 timeout.

为什么发生

  • 连接 pool 是也 小 for current 加载

  • 连接是 是 leaked (不 returned 到 pool)

  • 长-运行查询是 tying 上连接

  • 数据库是 慢 or unreachable, causing 连接到 hang

最小示例

✗ 错误代码
// Connection not returned to the pool
const conn = await pool.getConnection();
const result = await conn.query('SELECT * FROM users');
// forgot conn.release() → connection leak
✓ 修复代码
const conn = await pool.getConnection();
try {
  const result = await conn.query('SELECT * FROM users');
  return result;
} finally {
  conn.release(); // always release
}

Using try/最后 ensures 连接是 总是 returned 到 pool, 偶数如果查询失败.

如何诊断

  • 检查 pool 大小 and current 连接数量

  • Look for 连接 leaks — 查询那个做 不发布连接

  • 检查 for 长-运行查询那个持有连接

  • Monitor pool wait 次数 and 连接 usage 模式

如何修复

  • Increase 连接 pool 大小

  • Fix 连接 leaks — ensure 所有连接是 released

  • 添加查询 timeouts 到 prevent 长-运行查询从 持有连接

  • Optimize 慢查询到 free 上连接 faster

如何预防

  • 总是 use try/最后 or 连接 pool helpers 那个 auto-发布连接

  • 设置 appropriate 查询 timeouts

  • Monitor pool metrics and adjust pool 大小 based on 加载

  • Use 异步/等待模式那个 automatically 返回连接

相关资源

相关术语

相关课程

← 返回语言