Path: redb/redb.Core/Data/RedbConnectionLease.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Data/RedbConnectionLease.cs
----
using System;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
namespace redb.Core.Data
{
///
/// The connection a redb connection works on, lent for the caller's own commands: the open connection, the
/// transaction the commands must run in, and a command factory bound to both. Taken by
/// , given back by .
///
/// The connection and the transaction stay redb's (or the ambient transaction's). While the lease is held the
/// holder must never: commit, roll back or dispose the transaction, or begin one of its own; close, dispose or
/// reopen the connection, or change its database; change session settings (SET, PRAGMA, isolation level);
/// leave a data reader open past the release; call redb on the same connection or in the same ambient
/// transaction (the call is refused at once); swallow a failure of its own command and go on with the
/// transaction (on PostgreSQL a failed command aborts the transaction for every later command).
///
///
public sealed class RedbConnectionLease : IAsyncDisposable
{
private const string NotOpenMessage =
"The connection of a redb lease was not open when the lease was released: the holder closed it, or it " +
"broke under the holder's command. The connection belongs to redb or to the ambient transaction; the " +
"holder must not close, dispose or reopen it.";
private readonly Action _release;
private int _released;
///
/// A lease over . gives the connection back (leaves
/// the command gates the lease entered); it runs once and must not close the connection.
///
/// The open connection the commands run on.
/// The transaction the commands must be bound to, or null when the driver runs
/// them in the connection's transaction by itself (an enlisted connection, or no transaction).
/// Gives the connection back; called once by .
public RedbConnectionLease(DbConnection connection, DbTransaction? transaction, Action release)
{
Connection = connection ?? throw new ArgumentNullException(nameof(connection));
Transaction = transaction;
_release = release ?? throw new ArgumentNullException(nameof(release));
}
/// The open connection. Not the holder's: never close, dispose or reopen it.
public DbConnection Connection { get; }
///
/// The transaction every command of the holder must be bound to: redb's explicit transaction, or on SQLite
/// inside a the transaction redb runs on the scope's
/// connection. Null when the connection was enlisted in the ambient transaction by its driver (PostgreSQL,
/// SQL Server) or when no transaction is active: commands then need none.
/// binds it already.
///
public DbTransaction? Transaction { get; }
/// A command on , bound to . The holder disposes it.
public DbCommand CreateCommand()
{
if (Volatile.Read(ref _released) != 0)
throw new ObjectDisposedException(nameof(RedbConnectionLease), "The lease was already released.");
var command = Connection.CreateCommand();
command.Transaction = Transaction;
return command;
}
///
/// Gives the connection back. The second call does nothing. Throws
/// after the release when the connection is no longer open: the holder closed it, or it broke.
///
public ValueTask DisposeAsync()
{
if (Interlocked.Exchange(ref _released, 1) != 0)
return default;
var open = Connection.State == ConnectionState.Open;
_release();
if (!open)
throw new InvalidOperationException(NotOpenMessage);
return default;
}
}
}