rlsTransaction function
InterceptorFunction
rlsTransaction({
- required PostgresConnectionPool pool,
- required TenantResolver tenantResolver,
- required Response onUnresolved(
- Request request
- String settingName = 'app.tenant_id',
- CommitPredicate shouldCommit = commitOnSuccess,
Opens one transaction per request with the tenant applied by
set_config(settingName, tenant, true), and ends it deterministically.
- No tenant, no connection. A
nullor empty tenant short-circuits toonUnresolvedbefore any connection is borrowed. There is no optional mode. - Buffered bodies commit before the response is written, so a failing
COMMITbecomes an error response instead of a 200 already sent. Streambodies commit in the cleanup hook, after the last byte, so the stream can keep reading through the executor. A failing commit then reaches only the server-sideonError; such routes must be read-only.- Rollback when the handler throws, when
shouldCommitsays no, or afterRlsTransaction.setRollbackOnly. - The pool being exhausted answers 503 with
Retry-After(onUnavailable).
settingName is configuration, not a decision of this package: the
database policy must read the same name.
Implementation
InterceptorFunction rlsTransaction({
required PostgresConnectionPool pool,
required TenantResolver tenantResolver,
required Response Function(Request request) onUnresolved,
String settingName = 'app.tenant_id',
CommitPredicate shouldCommit = commitOnSuccess,
Response Function(Request request, PoolExhaustedException error)
onUnavailable = _defaultUnavailable,
}) =>
rlsTransactionOn(
unitOfWorkFactory: () => PostgresUnitOfWork(pool),
tenantResolver: tenantResolver,
onUnresolved: onUnresolved,
settingName: settingName,
shouldCommit: shouldCommit,
onUnavailable: onUnavailable,
);