I solved my problem.
In the stored procedure there is a ROLLBACK
and COMMIT
keywords. But there is no BEGIN TRANSACTION
anywhere in the procedure. From the beginning, I thought it is strange.
As you know COMMIT
decrements @@TRANCOUNT
by 1. Or to be more precise:
If @@TRANCOUNT is 1, COMMIT TRANSACTION makes all data modifications performed since the start of the transaction a permanent part of the database, frees the resources held by the transaction, and decrements @@TRANCOUNT to 0. If @@TRANCOUNT is greater than 1, COMMIT TRANSACTION decrements @@TRANCOUNT only by 1 and the transaction stays active.
In my case I begin a transaction in the code. And COMMIT
in the procedure is trying to commit my transaction and decrement @@TRANCOUNT
but it isn't completed yet.
So I added BEGIN TRANSACTION
to the stored procedure and it works fine.