Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit Transaction take too long?

I have a stored procedure that have the following code:

BEGIN TRY
--BEGIN TRANSACTION @TranName

    DECLARE @ID int

    INSERT INTO [dbo].[a] ([Comment],[Type_Id],[CreatedBy])
    VALUES ('test',1,2)

    SET @ID = SCOPE_IDENTITY()

    INSERT INTO [dbo].[b] ([Can_ID],[Com_ID],[Cal_ID],[CreatedBy])
    VALUES (1,@ID,null,2)

    UPDATE c SET LastUpdated = GETDATE(), LastUpdatedBy = 2 WHERE b.id = @ID

    --COMMIT TRANSACTION @TranName

    SELECT * from [View] where a.id=@ID
    END TRY
    BEGIN CATCH
--ROLLBACK TRANSACTION @TranName
END CATCH

Each of the statements in there running individually (as it is now) run fast. But when we remove the comments from the Transaction's piece of code the scripts run time increases from 1s to more than 2 minutes.

The system has been running for quite a while now, and this wasn't a problem before, I've been trying to search documentation about how SQL Server handle Transactions just in case there is anything that may affect SQL performance and the only thing that I have in mind is the Transaction Log... but ideally these individual statements run in a individual transaction as well, any idea?

like image 371
Noe Avatar asked Oct 18 '22 07:10

Noe


1 Answers

As Jens suggested The problems was because of some Tables blocking, after resetting SQL Server Service this locks disappeared and the DB started working properly again.

like image 62
Noe Avatar answered Oct 21 '22 02:10

Noe