I have stored procedure:
CREATE PROCEDURE [dbo].[mysp] @saID INT AS DECLARE @msg VARCHAR(4000), @message NVARCHAR(255), @counter INT BEGIN TRY SET @tranCounter=@@TRANCOUNT SET @message='SA ' + CAST(@saId AS NVARCHAR(10)) +' started by '+ CAST(@@spID AS nvarchar(10)) EXEC xp_logevent 60000, @message, informational IF @tranCounter=0 BEGIN BEGIN TRAN END UPDATE dbo.myTbl SET i=2 WHERE idN=10 AND i=1; IF @@ROWCOUNT=0 RAISERROR ('There is no active alignment.', 16, 1) INSERT INTO dbo.myTbl SELECT .... SET @counter=@@ROWCOUNT SET @message='SA '+ CAST(@saId AS NVARCHAR(10))+': Orders were reduced('+ CAST(@counter as nvarchar(20))+')! ' EXEC xp_logevent 60000, @message, informational MERGE dbo.myTbl1 AS t.... SET @counter=@@ROWCOUNT SET @message='SA '+ CAST(@saId AS NVARCHAR(10))+': Stock was increased('+ CAST(@counter as nvarchar(20))+')! ' EXEC xp_logevent 60000, @message, informational INSERT INTO dbo.myTbl2 SELECT .... SET @counter=@@ROWCOUNT SET @message='SA '+ CAST(@saId AS NVARCHAR(10))+': Stock for DD was reduced('+ CAST(@counter as nvarchar(20))+')! ' EXEC xp_logevent 60000, @message, informational EXEC @counter=dbo.p_stock_alignment @saId SET @message='SA '+ CAST(@saId AS NVARCHAR(10))+': Stock was aligned('+ CAST(@counter as nvarchar(20))+')! ' EXEC xp_logevent 60000, @message, informational ...
...
...IF @tranCounter=0 COMMIT TRAN SET @message='SA ' + CAST(@saId AS NVARCHAR(10))+': Was successfully finished!' EXEC xp_logevent 60000, @message, informational RETURN 0 END TRY BEGIN CATCH IF @tranCounter=0 AND @@TRANCOUNT>0 ROLLBACK TRAN SELECT @msg=ERROR_MESSAGE() EXEC msdb.dbo.sp_send_dbmail @profile_name='sql mail', @recipients='myemail@gmail.com', @subject='subject', @body_format ='HTML', @body=@msg SET @message='SA ' + CAST(@saId AS NVARCHAR(10))+': There was an error:'+ CAST(@msg AS NVARCHAR(200))+'! ' EXEC xp_logevent 60000, @message, ERROR RAISERROR (@msg,11,1) RETURN -1 END CATCH
Inside this procedure i execute in transaction many updates and inserts and every one is logged.
If I execute this procedure from SSMS(without outside transaction, so @trancounter would be 0 inside procedure), it is always successful and works less than a minute.
If i execute this procedure from application(web page, IIS 8.0, also without outside transaction), the procedure never ends successfully. It sometimes finished at step one, sometimes at step 5, but never ends, always is stopped somewhere in between.
And i also never get an error message.
So, catch is never raised and transaction is always rolled back (I guess, since there are no changes in my tables and there is no active transaction If i execute DBCC OPENTRAN)
It looks like that there is auto abort of transaction (XACT_ABORT is default ON for connections) and there is no error message, as i can't find anything in log and since I don't get error message the catch is also never raised.
Maybe IIS close connection or something similar after some time - but all settings i have found are more than a minute.
How can I find out what happened, why procedure never ends?
That started to happens when we moved to new server, windows 2012 and sql 2012.
Any idea?