Connection Pooling
SqbClient never talks to the database directly — it keeps a pool of physical connections
(built on the lightning-pool package) and hands
one out whenever you call acquire() or
execute().
Configuring the pool
Pass a pool object in ClientConfiguration:
const client = new SqbClient({
dialect: 'postgres',
host: 'localhost',
database: 'mydb',
pool: {
max: 20,
min: 2,
idleTimeoutMillis: 60000,
},
});
SqbClient's constructor reads the following fields off pool and coerces each one to its
proper type, substituting the default shown below whenever the field is missing or invalid:
| Option | Type | Default | Description |
|---|---|---|---|
acquireMaxRetries | number | 0 | How many times to retry creating a resource after a failed create() before giving up on an acquire() call. |
acquireRetryWait | number | 2000 | Milliseconds to wait between acquire retries. |
acquireTimeoutMillis | number | 0 | Milliseconds to wait for a resource before an acquire() call rejects. 0 means wait indefinitely. |
idleTimeoutMillis | number | 30000 | Milliseconds an idle connection can sit in the pool before being destroyed (subject to min/minIdle). |
max | number | 10 | Maximum number of connections the pool will create. |
maxQueue | number | 1000 | Maximum number of pending acquire() calls that may be queued once the pool is at max. |
min | number | 0 | Minimum number of connections the pool tries to keep open. |
minIdle | number | 0 | Minimum number of idle connections the pool tries to keep open. |
validation | boolean | false | Whether to call the adapter connection's test() method before handing it out from the pool. |
lightning-pool itself also supports fifo (queue order) and houseKeepInterval (housekeeper
tick interval) options, and defaults validation to true. SqbClient builds a brand-new pool
options object from only the nine fields above — it does not forward fifo or
houseKeepInterval from your pool config, and it explicitly defaults validation to false
rather than inheriting lightning-pool's own default of true. If you set pool.fifo or
pool.houseKeepInterval, they are currently silently ignored and the underlying pool falls back
to lightning-pool's own defaults for those two (fifo: true, houseKeepInterval: 1000).
Internally, the pool's factory is wired to the adapter you registered:
create()calls the adapter'sconnect(config).destroy()calls the adapter connection'sclose().reset()calls the adapter connection'sreset()(run whenever a connection is returned to the pool).validate()calls the adapter connection'stest()(only used whenvalidation: true).
Inspecting the pool
client.pool exposes the raw lightning-pool Pool instance, which is useful for
monitoring:
console.log(client.pool.size); // total connections (idle + acquired)
console.log(client.pool.acquired); // connections currently checked out
console.log(client.pool.available); // idle connections
console.log(client.pool.pending); // callers waiting for a connection
console.log(client.pool.state); // PoolState
SqbClient also re-emits some of the pool's lifecycle events on itself: closing, close,
terminate and error.
Closing the pool
await client.close();
close(terminateWait?: number) shuts the pool down and destroys every connection in it,
returning a promise that resolves once that's done. client.isClosed becomes true once the
close completes. Pass terminateWait (milliseconds) to bound how long the pool waits for
in-flight acquisitions to finish before forcibly terminating them — client.close(0), as used
throughout the test suite, terminates immediately.