Skip to content

连接 Pool

一句话解释

连接 pool maintains 设置的 打开数据库连接那个是 reused 跨越请求, avoiding cost 的打开新 连接.

用大白话说

A connection pool is a set of pre-opened database connections that the application reuses. Instead of opening a new connection for every request (which is slow and resource-intensive), the application borrows a connection from the pool, uses it, and returns it.

Opening a database connection involves TCP handshake, authentication, and session setup — this can take 10-100ms. With a pool, the first request pays this cost; subsequent requests reuse the warm connection in microseconds.

The pool has a configurable size (e.g., 10 connections). If all connections are in use, new requests either wait or fail. Pool sizing is a key performance tuning decision — too few connections cause waiting, too many can overwhelm the database.

示例

javascript
// Using pg Pool in Node.js
const { Pool } = require("pg");

const pool = new Pool({
  connectionString: "postgres://localhost/mydb",
  max: 20,              // max 20 connections
  idleTimeoutMillis: 30000,
});

// Borrow a connection, run a query, return it
const res = await pool.query("SELECT * FROM users WHERE id = $1", [1]);

Pool manages 连接. 每个 pool.查询 borrows, uses, and 返回连接 automatically.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表