Skip to content

feat: add foreignConnections and runOutsideTransaction; only reject foreign connections in open transactions - #826

Merged
gajus merged 1 commit into
mainfrom
gajus/foreign-connections
Nov 5, 2026
Merged

gajus merged 1 commit into
mainfrom
gajus/foreign-connections

Conversation

@gajus

@gajus gajus commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Fixes false positives of UnexpectedForeignConnectionError and replaces dangerouslyAllowForeignConnections with a non-boolean foreignConnections option.

Only reject foreign connections while the transaction is open

Async work that is started inside of a transaction inherits the transaction's async context, even when it runs after the transaction has ended. Previously, any query that such work ran on another connection raised UnexpectedForeignConnectionError, e.g. from:

  • a deferred side effect, timer or DataLoader batch that runs after COMMIT
  • a commit or rollback event listener, which runs inside of the transaction's context

The transaction context now tracks whether the transaction is open: it opens after START TRANSACTION (including when the transaction is retried) and closes after COMMIT or ROLLBACK, before the events are emitted. The check applies only while the transaction is open.

runOutsideTransaction

Runs a routine outside of the surrounding transaction, so that its queries can use other connections while the transaction is open, e.g. an audit log entry that must persist even if the transaction is rolled back:

import { runOutsideTransaction } from "slonik";

await pool.transaction(async (transactionConnection) => {
  await runOutsideTransaction(async () => {
    await pool.query(sql.typeAlias("void")`INSERT INTO audit_log (event) VALUES ('baz')`);
  });
});

foreignConnections

foreignConnections: "reject" (default) throws UnexpectedForeignConnectionError; foreignConnections: "allow" runs the query outside of the transaction. dangerouslyAllowForeignConnections: true keeps working as a deprecated alias of foreignConnections: "allow", and foreignConnections takes precedence when both are provided. Any other value throws InvalidConfigurationError.

Tests

  • Integration tests: queries from async work that runs after COMMIT, from commit and rollback listeners, inside of a retried transaction (still rejected), with runOutsideTransaction (not rolled back), and with foreignConnections: "allow".
  • Unit tests for resolving foreignConnections: default, deprecated alias, precedence, and an invalid value.

@changeset-bot

changeset-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3d35695

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
slonik Minor
@slonik/benchmark Minor
@slonik/driver Minor
@slonik/errors Minor
@slonik/pg-driver Minor
@slonik/dataloaders Minor
slonik-interceptor-field-name-transformation Minor
slonik-interceptor-query-cache Minor
slonik-interceptor-query-logging Minor
slonik-sql-tag-raw Minor
@slonik/sql-tag Minor
@slonik/test-ssls Minor
@slonik/types Minor
@slonik/utilities Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gajus
gajus merged commit 4f74b91 into main Nov 5, 2026
4 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 5, 2026

This branch was previously deployed

1 inactive deployment
release — 3d35695e Deployed Nov 5, 2026 by gajus via Test node_version:24 test_only:utilities #444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant