Path: redb-route/redb.Route.Sql/Connection/SqlConnections.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.Sql/Connection/SqlConnections.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Sql.Connection;
/// How a sql: producer path takes the connection of one exchange.
internal static class SqlConnections
{
///
/// The refusal of outputType=StreamList on a lent connection: the stream would keep a reader open on the
/// lender's connection after the lease is given back.
///
public static InvalidOperationException StreamOnLentConnection(string? dataSource)
=> new(
$"outputType=StreamList keeps a reader open after the endpoint, and dataSource={dataSource} lends a connection " +
"only for the time of the step: the reader would outlive the lease, on a connection that is not this endpoint's. " +
"Use outputType=SelectList, which reads the rows before the connection is given back.");
///
/// Refuses a lending factory (dataSource=redb) for a repository: a repository is called without an exchange to
/// find the lender through, and it commits its own statements.
///
public static void RefuseLending(ISqlConnectionFactory factory, string repository, string paramName)
{
if (factory.LendsConnection)
throw new ArgumentException(
$"{repository} keeps its rows on a connection of its own and commits them itself, and this connection factory " +
"lends a connection (dataSource=redb) to one sql: step of an exchange. Give the repository a factory with a " +
"connection string of its own (it may name the same database).", paramName);
}
///
/// A lent connection when the factory lends one (), otherwise a
/// connection of the step's own from , refused (and closed)
/// when an ambient transaction runs and the connection stays out of it.
///
public static async Task OpenAsync(
ISqlConnectionFactory factory, IExchange exchange, bool readOnly, CancellationToken ct)
{
if (factory.LendsConnection)
return await factory.LeaseAsync(exchange, readOnly, ct).ConfigureAwait(false);
var connection = await factory.CreateConnectionAsync(readOnly, ct).ConfigureAwait(false);
if (SqlAmbientTransaction.Refusal(connection) is { } refusal)
{
await connection.DisposeAsync().ConfigureAwait(false);
throw refusal;
}
return SqlConnectionLease.Owned(connection);
}
}