Repository navigation
Reject commands after SQL Server aborts their transaction - #4857
sanjaysingh wants to merge 13 commits into
Conversation
Those commands were running outside the transaction and auto-committing.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
@dotnet-policy-service agree |
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
…annot drop it. The command was treating a remaining internal transaction as the only sign of a server abort, and both of those paths clear that field.
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
paulmedynski
left a comment
There was a problem hiding this comment.
@sanjaysingh - Thanks for the great contribution - this is a corner case that we had missed! A couple of items to address before we can accept.
|
/azp run |
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
|
The API contract for specifying transactions for a command doesn't make a lot of sense to me. We maintain so much more complexity by allowing silent reversion to auto commit mode. Today, you can reference the same completed transaction indefinitely, even in new command objects and they'll all run in auto commit mode. This can open the door to bugs like this: static bool TryReserveStock(SqlConnection connection, SqlTransaction transaction)
{
using var command = new SqlCommand(
"""
UPDATE Inventory
SET Quantity = Quantity - 1
WHERE ProductId = @id AND Quantity > 0
""",
connection,
transaction);
command.Parameters.Add("@id", SqlDbType.Int).Value = 42;
if (command.ExecuteNonQuery() == 0)
{
transaction.Rollback();
return false;
}
return true;
}
static void PlaceOrder(SqlConnection connection)
{
using var transaction = connection.BeginTransaction();
// BUG: ignores failure, including the helper's rollback.
TryReserveStock(connection, transaction);
// Still explicitly references the transaction, but it is completed.
using var command = new SqlCommand(
"INSERT INTO Orders (ProductId) VALUES (@id)",
connection,
transaction);
command.Parameters.Add("@id", SqlDbType.Int).Value = 42;
command.ExecuteNonQuery();
transaction.Commit();
}The code intends to roll back the transaction, but we have no guard that prevents the transaction from still being used, so the second command executes in auto-commit. We should change the contract to disable silent reversion and require explicit transaction management. I would argue for changing the contract to address the issue in 8.0.0 and keeping this backwards compatible fix for <=7.1. Then the fix is a simple check: before running the command, check that the transaction is not completed and that it matches the command on the connection. These would now throw instead of silently switching to auto commit for the second execution: command.Transaction = transaction;
command.ExecuteNonQuery(); // Inside transaction.
transaction.Commit();
command.ExecuteNonQuery();command.Transaction = transaction;
command.ExecuteNonQuery(); // Inside transaction.
transaction.Rollback();
command.ExecuteNonQuery();Let me discuss with the team. |
priyankatiwari08
left a comment
There was a problem hiding this comment.
Minor concerns:
Commit()now drains/validates before the bulk-copy lock guard (inline).- Dispose of a server-aborted tx leaves the bound command rejected (inline).
Execute_AfterCommitDrainsAbandonedRollback_DoesNotAutocommitdiscards theCommitresult viaRecord.Exception; assert it throwsInvalidOperationException(zombied) so a regression can't pass silently.
|
I have addressed this. Thanks for catching these, @priyankatiwari08. |
priyankatiwari08
left a comment
There was a problem hiding this comment.
Re-reviewed 60ff71a. The bulk-copy guard ordering and commit-exception assertion are addressed. Two remaining cases are detailed inline: deferred rollback during adapter batching, and disposal after full zombification. Findings are based on source tracing; no local tests were run.
…saction is disposed.
priyankatiwari08
left a comment
There was a problem hiding this comment.
- Behavior regression:
Rollback()afterDispose()now throws (inline). - Missing async coverage for the core abort scenario (inline).
- mdaigle's contract/compat discussion (no auto-commit fallback) is still open; doc/switch decision pending.
…before rechecking them.
|
@priyankatiwari08 Disposal now clears only |
|
@paulmedynski |
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
…ts with Arrange, Act, and Assert.
|
@apoorvdeshmukh The 7 unit tests now have |
…ave-attestation check.
|
I merged |
A command bound to a transaction SQL Server had already aborted still ran and auto-committed.
The command now fails with the transaction-completed error. Assigning Connection again, or reading Transaction or IsolationLevel, keeps that aborted transaction attached. Rollback of the aborted transaction clears it. Disposing a transaction that is still live is a normal rollback, and the bound command can run again.
Fixes #4855
Test plan