Path: redb-route/redb.Route.Core/Sql/RedbSqlConnectionFactory.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Core/Sql/RedbSqlConnectionFactory.cs ---- using System.Data.Common; using redb.Route.Abstractions; using redb.Route.RedbCore.Extensions; using redb.Route.Sql.Connection; namespace redb.Route.RedbCore.Sql; /// /// The sql: data source redb: a step with dataSource=redb (the default redb service) or /// dataSource=redb:orders (the named one) runs its statement on the connection that service works on for the /// exchange, in the transaction redb's own commands run in. Inside .Transacted() the statement and every redb step /// of the block commit or roll back together, on one connection, without a distributed transaction; outside a /// transaction the statement runs in autocommit. /// /// The service is the one the redb steps of the same exchange get (), /// borrowed through IRedbConnection.LeaseAsync for the time of the statement and given back right after it. /// /// public sealed class RedbSqlConnectionFactory : ISqlConnectionFactory { /// The data source name the factory is registered under. public const string DataSourceName = "redb"; private readonly IRouteContext _context; private readonly string? _name; /// The factory of the default redb service on . /// The route context whose redb services lend their connection. public RedbSqlConnectionFactory(IRouteContext context) : this(context, name: null) { } private RedbSqlConnectionFactory(IRouteContext context, string? name) { _context = context ?? throw new ArgumentNullException(nameof(context)); _name = name; } /// public bool LendsConnection => true; /// Refused: the data source lends redb's connection per exchange and opens none of its own. public Task CreateConnectionAsync(bool readOnly = false, CancellationToken ct = default) => throw new NotSupportedException( $"dataSource={Describe(_name)} lends the connection of a redb service to one sql: step of an exchange and " + "opens no connection of its own. Use it on a sql: producer; a component that keeps a connection outside an " + "exchange (the polling sql: consumer, the SQL idempotent or claim-check repository) needs a data source with " + "a connection string."); /// /// The factory of the default service (no qualifier) or the named one (redb:orders). Throws when the service /// is not registered on the context, so a route with a missing or misspelled name fails at start. /// public ISqlConnectionFactory ForQualifier(string? qualifier) { var name = string.IsNullOrEmpty(qualifier) ? null : qualifier.TrimStart('#'); if (name is null) { if (!RedbRouteExtensions.IsDefaultRedbRegistered(_context)) throw new InvalidOperationException( "dataSource=redb runs the statement on the connection of the default redb service, and no IRedbService " + "is registered. Call services.AddRedb() / services.AddRedbPro(), or name a registered instance with " + "dataSource=redb:."); return _name is null ? this : new RedbSqlConnectionFactory(_context, null); } if (!RedbRouteExtensions.IsNamedRedbRegistered(_context, name)) throw new InvalidOperationException( $"dataSource=redb:{name} runs the statement on the connection of the redb service '{name}', and no such " + "service is registered on the route context. Register it via context.RegisterRedbService(name, instance) " + "or configure it in the Redb section."); return string.Equals(_name, name, StringComparison.Ordinal) ? this : new RedbSqlConnectionFactory(_context, name); } /// /// Borrows the connection of the exchange's redb service. is ignored: the statement runs /// where redb works, never on a replica. /// public async Task LeaseAsync(IExchange exchange, bool readOnly = false, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(exchange); var redb = _context.GetRedbService(_name ?? string.Empty, exchange); var lease = await redb.Context.Db.LeaseAsync(ct).ConfigureAwait(false); return SqlConnectionLease.Borrowed(lease.Connection, lease.Transaction, lease.DisposeAsync); } private static string Describe(string? name) => name is null ? DataSourceName : DataSourceName + ":" + name; }