Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing - SQL server 2005

I am getting the error from the application as following with SQL server 2005

"Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0"

How can I find the stage where this error is raised?

How can I find the missing transaction or the stored procedure where it is not committed or getting rolled back?

like image 658
Paresh Avatar asked Feb 15 '10 11:02

Paresh


People also ask

What is the value of @@ Trancount after begin transaction is executed for the second time?

The current nesting level is held in the global variable @@trancount. The @@trancount variable has a value of zero before a BEGIN TRANSACTION statement is executed, and only a COMMIT executed when @@trancount is equal to one makes changes to the database permanent.

How do I count transactions in SQL Server?

@@TRANCOUNT (Transact-SQL) Returns the number of BEGIN TRANSACTION statements that have occurred on the current connection.

How do you do the rollback if any failure happens in transaction?

By setting XACT_ABORT to ON and we can rollback all the statements inside a transaction when an error occurred. Thus, let's rewrite the code again in this manner. It will also roll back the transaction when the error occurred in the third statement.


2 Answers

I don't think anything is missing. It's probably an inner stored procedure that gets called from inside a transaction (TRANCOUNT = 1), starts its own transaction (TRANCOUNT = 2) and then rolls it back. Well, it means to roll it back, but rollback affects all transactions and not only the innermost one, so the procedure screws up the execution flow.

A way to find the place depends on available tools/skills. A better way is to use SQL Profiler that shows all commands executed by an application against the server. Find out the outermost stored procedure and go through its code looking for any other procedure calls.

like image 101
GSerg Avatar answered Oct 21 '22 12:10

GSerg


The system function @@TRANCOUNT will return how many transactions you are currently in. As part of your investigation, insert PRINT @@TRANCOUNT or SELECT @@TRANCOUNT statements at appropriate places to see what is going wrong.

like image 22
AakashM Avatar answered Oct 21 '22 14:10

AakashM