rlsTransaction function

InterceptorFunction rlsTransaction({
  1. required PostgresConnectionPool pool,
  2. required TenantResolver tenantResolver,
  3. required Response onUnresolved(
    1. Request request
    ),
  4. String settingName = 'app.tenant_id',
  5. CommitPredicate shouldCommit = commitOnSuccess,
  6. Response onUnavailable(
    1. Request request,
    2. PoolExhaustedException error
    ) = _defaultUnavailable,
})

Opens one transaction per request with the tenant applied by set_config(settingName, tenant, true), and ends it deterministically.

  • No tenant, no connection. A null or empty tenant short-circuits to onUnresolved before any connection is borrowed. There is no optional mode.
  • Buffered bodies commit before the response is written, so a failing COMMIT becomes an error response instead of a 200 already sent.
  • Stream bodies 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-side onError; such routes must be read-only.
  • Rollback when the handler throws, when shouldCommit says no, or after RlsTransaction.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,
    );