Path: redb-route/redb.Route/Definitions/TransactionDefinition.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/Definitions/TransactionDefinition.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.ErrorHandling; using redb.Route.Processors; using redb.Route.Transactions; namespace redb.Route.Definitions; /// /// Scope-opener definition for declarative transaction management. /// All child steps execute inside a which: /// /// Wraps the inner pipeline in a . /// Auto-commits all instances on success. /// Auto-rolls-back on exception or cancellation. /// /// No explicit CommitTransaction() / RollbackTransaction() needed. /// Inherits the leaf DSL from ; /// close with or the universal . /// public class TransactionDefinition : RouteDefinitionBase, IRouteScope, IDurableScope { private readonly TransactionPolicy? _policy; private int _retryAttempts; private TimeSpan _retryDelay; private string? _deadLetterUri; /// The transaction policy this scope will use (null = default Required). public TransactionPolicy? Policy => _policy; internal TransactionDefinition(TransactionPolicy? policy = null) { _policy = policy; } // ── Camel-parity error handler hooks ─────────────────────────────────────── /// /// Apache Camel parity: errorHandler(defaultErrorHandler().maximumRedeliveries(n).redeliveryDelay(d)). /// Retries the inner transactional body up to times before letting /// the exception propagate to the transaction (which will then roll back). /// public TransactionDefinition Retry(int attempts, TimeSpan delay) { if (attempts <= 0) throw new ArgumentOutOfRangeException(nameof(attempts)); _retryAttempts = attempts; _retryDelay = delay; return this; } /// /// Apache Camel parity: errorHandler(deadLetterChannel("...")). /// On failure the exchange is sent to after the transaction /// has rolled back. /// public TransactionDefinition DeadLetterChannel(string deadLetterUri) { ArgumentException.ThrowIfNullOrWhiteSpace(deadLetterUri); _deadLetterUri = deadLetterUri; return this; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes this transaction scope and returns the parent route definition. /// Thrown if no parent route is set. public IRouteDefinition EndTransaction() => (IRouteDefinition)(Parent ?? throw new InvalidOperationException( "EndTransaction() called without a parent route. Ensure Transaction() was called on a route definition.")); /// public IRouteDefinition End() => EndTransaction(); // ── IProcessorDefinition ─────────────────────────────────────────────────── /// public override IProcessor CreateProcessor(IRouteContext context) { IProcessor body = BuildPipeline(Outputs, context); var policy = _policy ?? new TransactionPolicy(); var logger = context.GetService()?.CreateLogger(); IProcessor txProcessor = new TransactedProcessor(body, policy, logger); // Retry wraps the transaction instead of living inside it: one attempt is one unit of work. A failed attempt // rolls back its own database work and its own deferred sends, so the attempt that succeeds sends only its own // messages, and no transaction stays open across the retry delays. if (_retryAttempts > 0) { var retryLogger = context.GetService()?.CreateLogger(); txProcessor = new RetryProcessor(txProcessor, RetryPolicy.Fixed(_retryAttempts, _retryDelay), retryLogger); } if (_deadLetterUri is not null) { var dlcLogger = context.GetService()?.CreateLogger(); txProcessor = new DeadLetterChannelProcessor(context, txProcessor, _deadLetterUri, rethrow: false, logger: dlcLogger); } return txProcessor; } private static IProcessor BuildPipeline(IList outputs, IRouteContext context) => NodePipeline.Body(context, outputs); }