I was assigned to make a stop button for our system transaction. Upon clicking the button STOP, I must be able to interrupt the stored procedure A and rollback. So I made a few checkers in my stored procedure.
Here's an example flow of my stored procedure:
START PROCEDURE
BEGIN TRANSACTION A
BUSINESS LOGIC...
-- STOP CHECKER
IF((SELECT STATUS FROM TABLENAME WITH(NOLOCK) WHERE CODE = @Code) = 'STOPPED')
GOTO STOP_FUNC
BUSINESS LOGIC...
-- STOP CHECKER
IF((SELECT STATUS FROM TABLENAME WITH(NOLOCK) WHERE CODE = @Code) = 'STOPPED')
GOTO STOP_FUNC
COMMIT TRANSACTION A
RETURN 1
STOP_FUNC :
BEGIN
ROLLBACK TRANSACTION A
RETURN -1000
END
END PROCEDURE
And here's my query for my Stop button :
UPDATE TABLENAME SET STATUS = 'STOPPED' WHERE CODE = '50'
I made a few test runs
TEST RUN 1
When executing the UPDATE first, the stored procedure will then stop at the first checker -- expected behaviour
TEST RUN 2
When executing the stored procedure first and executing the UPDATE
later, the UPDATE
goes to a deadlock according to the Activity Monitor. It will then wait for the SP to finish before updating the status as STOPPED.
I really am stuck right now. I know that the transaction may be locking my table but that's why I put the 'WITH (NOLOCK)' in there as advised by my coworkers. Can someone please advise a new workaround? I have little experience to this since I am originally a front end developer