Acquiring Connections
SqbClient.acquire() checks a connection out of the pool and hands you back a
SqbConnection — a wrapper around a single live
adapter connection that you use to run queries, manage transactions and read/change the schema.
acquire() is overloaded with two call forms.
Callback form (auto-release)
const result = await client.acquire(async connection => {
return connection.execute('select * from customers where id = $1', {
params: [1],
});
});
async acquire(fn: TransactionFunction, options?: ConnectionOptions): Promise<any>;
Pass a function and acquire() will:
- Check a connection out of the pool.
- Run your callback with that connection.
- Call
connection.release()in afinallyblock, whatever your callback returns or throws. - Resolve with whatever your callback returned (or reject with whatever it threw).
This is the recommended form for one-off units of work — you can't accidentally forget to release the connection.
No-callback form (manual release)
const connection = await client.acquire();
try {
await connection.execute('select 1');
} finally {
connection.release();
}
async acquire(options?: ConnectionOptions): Promise<SqbConnection>;
Without a callback, acquire() resolves directly with the SqbConnection. You are responsible
for releasing it — typically in a finally block, as above. This form is useful when a
connection needs to stay open across multiple, non-contiguous calls (for example, when a cursor
must outlive the function that opened it — see Cursors & Streaming).
ConnectionOptions currently has one field:
| Field | Type | Description |
|---|---|---|
autoCommit | boolean | Default autoCommit behavior applied to every query executed on this connection (can still be overridden per-call — see Executing Queries). |
Reference counting: retain() / release() / refCount
A SqbConnection starts life with an internal reference count of 1. Instead of a hard
open/close, SqbConnection uses reference counting to decide when the underlying adapter
connection actually goes back to the pool:
connection.retain()incrementsrefCountby one and emits aretainevent.connection.release()decrementsrefCountby one, emits areleaseevent, and — oncerefCountreaches0— callsconnection.close()to return the physical connection to the pool. It returnstrueif the connection was actually closed by that call,falseotherwise.connection.refCountreads the current count.
You rarely need to call retain() yourself in application code — SqbConnection calls it
internally around every execute() call (so a query can't return the connection to the pool
mid-flight), and SqbClient.execute() calls it once more when a query returns a
Cursor, so the connection stays open until the cursor is closed.
Call it yourself only if you need to keep a connection alive across an acquire() callback
boundary or similar.
connection.close() releases the underlying adapter connection back to the pool immediately,
regardless of refCount, and emits a close event.
sessionId and inTransaction
connection.sessionId— an adapter-assigned identifier for the underlying session, useful for logging/debugging.connection.inTransaction—trueif a transaction is currently open on this connection (see Transactions).
Testing a connection
await connection.test();
Delegates to the adapter connection's own test() method — useful for health checks. SqbClient
also exposes client.test(), which acquires a connection, calls test() on it, and releases it.