Path: redb-route/redb.Route.Sql/Batch/SavepointBatchWriter.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/Batch/SavepointBatchWriter.cs
----
using System.Data.Common;
using Microsoft.Extensions.Logging;
using redb.Route.Sql.Mapping;
namespace redb.Route.Sql.Batch;
///
/// Continue-past-errors batch (breakBatchOnError=false). Every item runs under its own savepoint: a failed item is
/// rolled back to it and reported, the others stay. Whether savepoints work is learnt by using them — providers under-report
/// (SqlClient says false and supports them). When rolling back to the
/// savepoint fails, the server has already ended the transaction; SQL Server would then run the next statement in
/// autocommit, so the batch stops at that item instead of sending anything more. The items share one command whose
/// parameter values are replaced per item; a DbBatch is never used here, since it cannot go on past a failed command.
/// A failure of the source itself is not an item error: it ends the batch. Rows a failed item returned are dropped with it.
///
internal sealed class SavepointBatchWriter(SqlEndpointOptions options, ILogger? logger) : IBatchWriter
{
/// Name of the per-item savepoint.
internal const string SavepointName = "redb_batch_item";
///
public string Strategy => "Savepoints";
///
public async Task WriteAsync(BatchWriteRequest request, CancellationToken ct)
{
var transaction = request.Transaction
?? throw new InvalidOperationException("Continuing past a failed batch item needs a local transaction.");
var outcome = new BatchOutcome();
await using var command = new BatchItemCommand(request, options);
while (await request.Items.ReadAsync().ConfigureAwait(false))
{
var index = request.Items.Index;
await SaveAsync(request, transaction, ct).ConfigureAwait(false);
var keysBefore = request.Keys?.Count ?? 0;
int affected;
try
{
affected = await command.ExecuteAsync(request.Items.Current, index, ct).ConfigureAwait(false);
}
// A returned row that cannot be mapped is the endpoint's configuration, not this item's failure: it goes up as is.
catch (Exception ex) when (ex is not OperationCanceledException && !ct.IsCancellationRequested && ex is not SqlRowMappingException)
{
if (!await TryRollbackToSavepointAsync(transaction, index).ConfigureAwait(false))
throw new BatchItemFailedException(index, ex);
request.Keys?.Truncate(keysBefore);
outcome.Errors.Add(new SqlBatchItemError(index, ex.Message, (ex as DbException)?.SqlState));
continue;
}
await transaction.ReleaseAsync(SavepointName, ct).ConfigureAwait(false);
outcome.RowsAffected += affected;
outcome.ItemsWritten++;
}
return outcome;
}
private static async Task SaveAsync(BatchWriteRequest request, DbTransaction transaction, CancellationToken ct)
{
try
{
await transaction.SaveAsync(SavepointName, ct).ConfigureAwait(false);
}
catch (NotSupportedException ex)
{
throw new InvalidOperationException(
$"breakBatchOnError=false undoes a failed item to a savepoint, and {request.Connection.GetType().Name} does not " +
"support savepoints. Use breakBatchOnError=true: the whole batch then rolls back on the first error.", ex);
}
}
private async Task TryRollbackToSavepointAsync(DbTransaction transaction, int index)
{
try
{
await transaction.RollbackAsync(SavepointName, CancellationToken.None).ConfigureAwait(false);
return true;
}
catch (Exception ex)
{
// Not swallowed: the caller stops the batch and rethrows the item's own error; this records why.
logger?.LogWarning(ex,
"SQL batch item {Index} failed and its savepoint can no longer be rolled back to; the transaction has ended, the batch stops",
index);
return false;
}
}
}