Forum Discussion
HELJEEVE
Oct 27, 2023Copper Contributor
CONCURRENCY IN SQLSERVER
MY BETOPTIONS TABLE DESIGN
ID COUNT
1 100
2 200
3 45
4 2
LIKE THIS
MY STORED PROCEDURE IS LIKE THIS
CREATE PROCEDURE BUYLOTTOGAME
BEGIN TRANS
SELECT @COUNT=COUNT FROM TABLENAME WITH UPDLOCK WHERE ID=@ID
IF @COUNT>=@SOMECOUNT
BEGIN
ROLLBACK TRAN
RETURN
END
UPDATE TABLENAME SET COUNT=COUNT+@INCONT WHERE ID=@ID
COMMIT TRAN
WILL THIS CODE HANDLE CONCURRENCY ISSUE WITHOUT DEADLOCK
INDEX NON CLUSTERED INDEX ON ID
- olafhelperBronze Contributor
HELJEEVE , what could case here a deadlock and why do you expect one?
A deadlock can only occur if two or more process accesses two or more resources and this in different order; that's here not the case.
- HELJEEVECopper Contributorhere concurrent transaction will trying to update the same row, that's why i am using WITH UPLCOK IN SELECT STATEMENT ,JUST LIKE FOR UPDATE OF in ORACLE. So when one transaction acquire a lock on a row , next transaction will wait till current transaction COMMITS THE TRANSACTION.
IN SQL SERVER 2005 THIS WAS WORKING PERFEECTLY EVEN WITH MORE TRANSACTIONS
THERE IS ONE MORE COLUMN IN THE TABLE
TIMESTAMP.
IN 2019 LOTS OF DEADLOCK IS COMMING WHEN USING THIS UPDLCOK
ANY SUGGESTION ON THIS
THANK YOU- olafhelperBronze Contributor