Path: redb/redb.Core/Data/RollbackAfterFailure.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/RollbackAfterFailure.cs
----
using System;
using System.Threading.Tasks;
namespace redb.Core.Data
{
///
/// Rolls back a transaction after the work in it failed, without letting a failed rollback replace the failure.
///
/// SQL Server ends the transaction itself on a command timeout, on a deadlock victim (1205) and on any error that
/// dooms it; the client's rollback then throws "This SqlTransaction has completed". Rethrown from the catch, that
/// error hid the SqlException the caller needs: its message, its Number and the deadlock retry that looks for 1205.
/// The rollback error is kept on the original exception under .
///
///
internal static class RollbackAfterFailure
{
/// Key in of the original exception that holds the rollback error.
internal const string DataKey = "redb:RollbackFailure";
///
/// Rolls back; an exception of the rollback is attached to
/// and swallowed, so the caller rethrows the original as it was.
///
internal static async Task RollbackAsync(IRedbTransaction transaction, Exception original)
{
try
{
await transaction.RollbackAsync();
}
catch (Exception rollbackFailure)
{
try { original.Data[DataKey] = rollbackFailure; }
catch (Exception) { /* an exception type with a read-only Data keeps only the original */ }
}
}
}
}