Class AmbientConnectionRegistry

Namespace: redb.Core.Data
Assembly: redb.Core.dll

One physical connection per database per ambient (plan
docs/V4/AMBIENT_TRANSACTION_PLAN.md, 2026-09-14).

A redb.Route .Transacted() block is a , and everything it writes -
redb objects, raw SQL through redb.Context, a store resolving a scope of its own, the key
generator - must land in that one transaction. Two connections open at the same time inside it turn
it into a distributed transaction: SQL Server refuses it, PostgreSQL aborts at commit, and SQLite
never takes part in System.Transactions at all. So the TRANSACTION holds the connection: every redb
connection wrapper of the same database asks this registry for it while the transaction is ambient.

The connection is opened inside the transaction on first use, so SqlClient and Npgsql enlist
it themselves.A provider whose driver cannot enlist (SQLite) begins its own transaction on it; the entry
enlists as a volatile resource and commits or rolls that transaction back with the scope.The entry is released when the transaction completes.Dependent clones share the transaction and therefore the entry: two commands at the same time
are refused (owner decision: parallel branches in one transaction are not supported).

public static class AmbientConnectionRegistry

Inheritance

Object→AmbientConnectionRegistry

Properties

Count

Number of live entries (diagnostics and tests).

public static int Count { get; }

Methods

Acquire(Transaction, string, string, Func<DbConnection>, Func<DbConnection, DbTransaction>?)

Synchronous for the thread-pool-free paths.

public static AmbientLease Acquire(Transaction transaction, string databaseKey, string sessionSignature, Func<DbConnection> open, Func<DbConnection, DbTransaction>? beginOwnTransaction)

AcquireAsync(Transaction, string, string, Func<CancellationToken, Task<DbConnection>>, Func<DbConnection, DbTransaction>?, CancellationToken)

The connection of transaction for databaseKey, opened on first

public static Task<AmbientLease> AcquireAsync(Transaction transaction, string databaseKey, string sessionSignature, Func<CancellationToken, Task<DbConnection>> openAsync, Func<DbConnection, DbTransaction>? beginOwnTransaction, CancellationToken cancellationToken = default)

GetOrOpen(Transaction, string, string, Func<DbConnection>, Func<DbConnection, DbTransaction>?)

Synchronous .

public static AmbientConnection GetOrOpen(Transaction transaction, string databaseKey, string sessionSignature, Func<DbConnection> open, Func<DbConnection, DbTransaction>? beginOwnTransaction)

GetOrOpenAsync(Transaction, string, string, Func<CancellationToken, Task<DbConnection>>, Func<DbConnection, DbTransaction>?, CancellationToken)

The entry without entering its gate - for work that runs between commands of the same flow

public static Task<AmbientConnection> GetOrOpenAsync(Transaction transaction, string databaseKey, string sessionSignature, Func<CancellationToken, Task<DbConnection>> openAsync, Func<DbConnection, DbTransaction>? beginOwnTransaction, CancellationToken cancellationToken = default)

TryGet(Transaction, string)

The live entry of transaction for the database, or null when none was opened.

public static AmbientConnection? TryGet(Transaction transaction, string databaseKey)