Path: redb-route/redb.Route/Transactions/BeginTransactionProcessor.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/Transactions/BeginTransactionProcessor.cs
----
using System.Collections.Concurrent;
using System.Transactions;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
namespace redb.Route.Transactions;
///
/// Imperative transaction processor that opens a
/// and stores it on the exchange for later commit/rollback.
///
/// Use or
/// to explicitly close the scope. If neither is called, the scope will be
/// disposed (rolled back) when the exchange completes.
///
///
public sealed class BeginTransactionProcessor : IProcessor
{
/// Well-known exchange property key for the active .
internal const string ScopePropertyKey = "TRANSACTION_SCOPE";
///
/// Set when the block is a unit of work of its own: holds the enclosing block's set of deferred actions (or a marker
/// for none), which its commit or rollback puts back. Absent when the block joined an enclosing one.
///
internal const string EnclosingActionsPropertyKey = "TRANSACTION_ENCLOSING_ACTIONS";
private static readonly object NoEnclosingActions = new();
private readonly TransactionPolicy _policy;
private readonly ILogger? _logger;
/// Creates a begin-transaction processor.
/// Transaction policy (scope option, timeout, isolation level).
/// Optional logger.
public BeginTransactionProcessor(TransactionPolicy? policy = null, ILogger? logger = null)
{
_policy = policy ?? TransactionPolicy.Default;
_logger = logger;
}
///
public Task Process(IExchange exchange, CancellationToken ct = default)
{
// The same rules as .Transacted(): a Required block inside a running transaction joins it and leaves its sends to
// the enclosing block; otherwise the block keeps a set of its own until CommitTransaction or
// RollbackTransaction, which puts the enclosing set back, so a transacted send after the block is refused
// instead of deferred into a set nobody commits.
var joins = TransactedActions.JoinsEnclosingBlock(exchange, _policy);
var scope = _policy.CreateScope();
exchange.Properties[ScopePropertyKey] = scope;
if (!joins)
exchange.Properties[EnclosingActionsPropertyKey] = (object?)TransactedActions.Open(exchange) ?? NoEnclosingActions;
_logger?.LogDebug(
"Transaction started imperatively (ScopeOption={ScopeOption}, IsolationLevel={IsolationLevel}, Joined={Joined}).",
_policy.ScopeOption, _policy.IsolationLevel, joins);
return Task.CompletedTask;
}
///
/// Takes the block's own set off the exchange. Returns true, with the enclosing block's set or null,
/// when the block was a unit of work of its own; false when it joined an enclosing block.
///
internal static bool TryTakeOwnActions(IExchange exchange, out ConcurrentDictionary? enclosing)
{
enclosing = null;
if (!exchange.Properties.TryGetValue(EnclosingActionsPropertyKey, out var raw))
return false;
exchange.Properties.Remove(EnclosingActionsPropertyKey);
enclosing = raw as ConcurrentDictionary;
return true;
}
}