Path: redb-route/redb.Route.Sql/Connection/SqlConnectionLease.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/SqlConnectionLease.cs
----
using System.Data.Common;
namespace redb.Route.Sql.Connection;
///
/// The connection one sql: step runs on: an open connection, the transaction its commands must be bound to, and
/// how it is given back. wraps a connection of the step's own, closed on release;
/// wraps a connection someone else owns (redb's, through dataSource=redb), handed back on release and never closed.
///
public sealed class SqlConnectionLease : IAsyncDisposable
{
private readonly Func _release;
private int _released;
private SqlConnectionLease(DbConnection connection, DbTransaction? transaction, bool isBorrowed, Func release)
{
Connection = connection;
Transaction = transaction;
IsBorrowed = isBorrowed;
_release = release;
}
/// A connection of the step's own: disposes it.
/// The opened connection.
public static SqlConnectionLease Owned(DbConnection connection)
{
ArgumentNullException.ThrowIfNull(connection);
return new SqlConnectionLease(connection, null, isBorrowed: false, connection.DisposeAsync);
}
///
/// A connection the step does not own. gives it back once and must not close it. The step
/// begins no transaction of its own on it, binds every command to , and keeps no reader
/// open past the release.
///
/// The open connection.
/// The transaction the commands must be bound to; null when the driver runs them in the
/// connection's transaction by itself (an enlisted connection) or none is active (autocommit).
/// Gives the connection back; called once by .
public static SqlConnectionLease Borrowed(DbConnection connection, DbTransaction? transaction, Func release)
{
ArgumentNullException.ThrowIfNull(connection);
ArgumentNullException.ThrowIfNull(release);
return new SqlConnectionLease(connection, transaction, isBorrowed: true, release);
}
/// The open connection.
public DbConnection Connection { get; }
/// The transaction every command must be bound to; null when none is needed. binds it.
public DbTransaction? Transaction { get; }
/// True when the connection is lent (): the step neither owns it nor its transaction.
public bool IsBorrowed { get; }
/// A command on , bound to . The caller disposes it.
public DbCommand CreateCommand()
{
if (Volatile.Read(ref _released) != 0)
throw new ObjectDisposedException(nameof(SqlConnectionLease), "The lease was already released.");
var command = Connection.CreateCommand();
command.Transaction = Transaction;
return command;
}
/// Gives the connection back: disposes an owned one, hands a borrowed one back. The second call does nothing.
public ValueTask DisposeAsync()
=> Interlocked.Exchange(ref _released, 1) != 0 ? default : _release();
}