Why This Error Happens
Error 6401 occurs when a ROLLBACK TRANSACTION or ROLLBACK WORK statement references a transaction or savepoint name that does not exist in the current session’s transaction stack. This typically happens due to a mismatch in nested transaction naming, a savepoint being created in a different connection/session scope, or application code that attempts to roll back after a transaction has already been committed or rolled back implicitly (such as after a severe error or connection reset). It can also surface when connection pooling reuses a session that has lost its transaction context.
Step-by-Step Resolution Guide
Step 1: Audit the Transaction and Savepoint Names in Code
- Open the stored procedure, trigger, or application code referencing the failing transaction name.
- Verify that
SAVE TRANSACTION savepoint_nameandROLLBACK TRANSACTION savepoint_nameuse identical, case-sensitive string values. - Check for typos or dynamically generated names (e.g., via string concatenation) that may not match at runtime.
Step 2: Check for Premature Transaction Completion
- Review
@@TRANCOUNTbefore the rollback statement usingPRINT @@TRANCOUNTfor debugging. - Confirm that no prior
COMMITorROLLBACKhas already closed the transaction before the named rollback executes. - Inspect
TRY...CATCHblocks to ensure error handling logic does not fire multiple rollback attempts on the same transaction.
Step 3: Validate Connection and Session Scope
- Ensure the savepoint was created and is being rolled back within the exact same database connection/session (
SPID). - Check
Connection Poolingsettings in your application’s connection string, located typically inweb.configorappsettings.json, since pooled connections can reset transaction context unexpectedly. - If using MARS (Multiple Active Result Sets), verify that
MultipleActiveResultSets=Truein the connection string is not causing transaction state conflicts across batches.
Step 4: Review SQL Server Error Logs
- Navigate to
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGon Windows Server 2025. - Search for Error 6401 timestamps and correlate them with the specific query or stored procedure execution to isolate the failing transaction block.
- Enable
Trace Flag 3604temporarily in SQL Server Configuration Manager to output additional transaction diagnostics to the console if reproducing in a test environment.
Alternative or Verification Steps
- Run the corrected stored procedure or script manually in SQL Server Management Studio (SSMS) and confirm
@@TRANCOUNTreturns to0after execution without triggering Error 6401. - If the issue persists intermittently, wrap rollback logic in a conditional check such as
IF XACT_STATE() = -1 ROLLBACK TRANSACTION savepoint_nameto prevent rollback attempts on already-doomed or completed transactions.