Path: redb/redb.Tests.Integration/Tests/MsSql/MsSqlSoftDeleteProcedureTests.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.Tests.Integration/Tests/MsSql/MsSqlSoftDeleteProcedureTests.cs
----
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Fixtures;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.MsSql;
///
/// sp_mark_for_deletion runs under SET XACT_ABORT ON (trash review, 2026-09-14). Without it a client
/// cancel or a command timeout arriving inside the procedure's transaction bypasses CATCH and leaves the
/// transaction open on the session - after the connection returns to the pool as well - so every later
/// statement of that session runs inside it and is rolled back with it. The pin blocks the procedure on
/// a row lock held by another session, lets the command time out right there, and checks the session.
///
[Collection("MsSql")]
public class MsSqlSoftDeleteProcedureTests
{
private readonly MsSqlFixture _fixture;
public MsSqlSoftDeleteProcedureTests(MsSqlFixture fixture) => _fixture = fixture;
private static string ConnString() =>
new SqlConnectionStringBuilder(
new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build()
.GetConnectionString("MSSql")!)
{ Pooling = false }.ConnectionString;
[Fact]
public async Task MarkForDeletion_TimedOutInsideItsTransaction_LeavesNoTransactionOnTheSession()
{
await _fixture.Redb.SyncSchemeAsync();
var id = await _fixture.Redb.SaveAsync(new RedbObject
{ name = "trash-xact-abort-probe", Props = new TrashTargetProps { Title = "probe" } });
var userId = _fixture.Redb.SecurityContext.GetEffectiveUser().Id;
await using var holder = new SqlConnection(ConnString());
await holder.OpenAsync();
await using var held = (SqlTransaction)await holder.BeginTransactionAsync();
await using (var hold = new SqlCommand("UPDATE [dbo].[_objects] SET [_note] = N'held' WHERE [_id] = @id", holder, held))
{
hold.Parameters.AddWithValue("@id", id);
await hold.ExecuteNonQueryAsync();
}
await using var session = new SqlConnection(ConnString());
await session.OpenAsync();
await using (var mark = new SqlCommand(
"DECLARE @trash BIGINT, @marked BIGINT; EXEC [dbo].[sp_mark_for_deletion] @ids, @user, NULL, @trash OUTPUT, @marked OUTPUT;",
session) { CommandTimeout = 2 })
{
mark.Parameters.AddWithValue("@ids", id.ToString());
mark.Parameters.AddWithValue("@user", userId);
var act = async () => await mark.ExecuteNonQueryAsync();
await act.Should().ThrowAsync("the procedure waits on the held row until the command times out");
}
await using (var probe = new SqlCommand("SELECT @@TRANCOUNT", session))
Convert.ToInt32(await probe.ExecuteScalarAsync()).Should().Be(0,
"a timeout inside the procedure must roll its transaction back, not leave it on the session");
await held.RollbackAsync();
(await _fixture.Redb.Context.ExecuteScalarAsync($"SELECT _id_scheme FROM _objects WHERE _id = {id}"))
.Should().NotBe(-10, "the timed-out mark changed nothing");
await _fixture.Redb.DeleteAsync(id);
}
}