Troubleshooting Guide: Error 3905 — Nested Transaction Limit Exceeded in SQL Server 2025
Why This Error Happens
Error 3905 occurs when a session’s @@TRANCOUNT value reaches its upper boundary because BEGIN TRANSACTION statements are being issued repeatedly without corresponding COMMIT or ROLLBACK calls. This is almost always caused by application-level connection pooling reusing a session that never properly closed an open transaction, or by poorly structured loops/recursive stored procedures that increment the transaction count on every iteration. In rare cases, ORM frameworks (like Entity Framework or Hibernate) mismanaging transaction scopes can silently stack nested BEGIN TRAN calls until the counter overflows.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Session and Query
- Run
DBCC OPENTRANin SQL Server Management Studio (SSMS) to identify long-running or orphaned transactions. - Query
sys.dm_tran_active_transactionsjoined withsys.dm_tran_session_transactionsto find the exactsession_idwith an abnormally high@@TRANCOUNT. - Cross-reference the
session_idwithsys.dm_exec_requeststo capture thesql_handleand extract the exact query text causing the issue.
Step 2: Audit and Correct Application/Procedure Code
- Open the stored procedure or application source code (e.g., C#, Java) identified in Step 1.
- Ensure every
BEGIN TRANSACTIONhas a matchingCOMMIT TRANSACTIONorROLLBACK TRANSACTION, specifically checkingTRY...CATCHblocks where aROLLBACKmight be missing after an exception. - If using nested procedures, replace manual
BEGIN TRANcalls with a check:IF @@TRANCOUNT = 0 BEGIN TRAN. - For connection pooling issues on Windows Server 2025, verify the application’s connection string includes
Enlist=falseif distributed transactions are not explicitly required.
Step 3: Terminate the Stuck Session and Reset the Pool
- If the transaction count is already maxed out and blocking production traffic, execute
KILL [session_id]in SSMS to forcibly terminate the connection and roll back the stack. - Restart the SQL Server (MSSQLSERVER) service via the Services console (
services.msc) on Windows Server 2025 ifKILLdoes not release the session within 30 seconds. - Clear the application-side connection pool (e.g., restarting the IIS Application Pool on Windows 11/Server 2025) to ensure a fresh, zero-trancount connection is established on the next request.
Step 4: Implement Trancount Safeguards
- Add a
SET XACT_ABORT ONstatement at the beginning of critical stored procedures to ensure automatic rollback on error, preventing the transaction count from climbing due to unhandled exceptions. - Deploy a scheduled SQL Agent Job that runs the
sys.dm_tran_session_transactionsquery hourly to alert administrators via Database Mail if any session’s trancount exceeds a safe threshold (e.g., 50).
Alternative or Verification Steps
- Verification: After deploying the code fix, run
SELECT @@TRANCOUNTimmediately after executing the specific stored procedure in a test environment; the result should return to0after the transaction logic completes. - Alternative: If the root cause remains elusive, enable a server-side trace or Extended Events session filtering on the
sqlserver.error_reportedevent whereerror_number = 3905to capture the exact T-SQL batch text in real-time for deeper analysis.