Exit stored procedure before using commit?

I tried to find this, but found nothing. If I have something like:

CREATE PROCEDURE QQ AS BEGIN TRANSACTION BEGIN TRY -- return early and skip commit here IF (Condition = true) RETURN 0 COMMIT TRANSACTION END TRY BEGIN CATCH ROLLBACK END CATCH 

What will happen to the transaction?

+4
source share
2 answers

It will remain open until you COMMIT , ROLLBACK , or the connection is closed and that recession is killed.

This will block other processes and cause all other problems.

As a rule, always check as shown below in the CATCH block to make sure that you have closed the cleanliness.

 IF @@TRANCOUNT > 0 ROLLBACK 

or

 WHILE @@Trancount > 0 BEGIN ROLLBACK END 
+5
source

To be sure, I must complete the ROLLBACK before you exit the instructions. Depending on your database software, they may perform an automatic commit on exit.

0
source

Source: https://habr.com/ru/post/1383712/


All Articles