Hi @Sudip Bhatt ,
Use of ROWLOCK:
- The ROWLOCK row-level lock ensures that when the user obtains the updated row, it will not be modified by other users during this time. Therefore, row-level locks can ensure data consistency and improve the concurrency of data operations.
- ROWLOCK tells SQL Server to only use row-level locks.
- Use in select statement
--A Execute in connection SET TRANSACTION ISOLATION LEVEL REPEATABLE READ begin tran select * from tablename with (rowlock,UpdLock) where id=3 waitfor delay '00:00:05' commit tran --If executed during B connection: update tablename set colname='10' where id=3 - then wait for 5 seconds update tablename set colname='10' where id <>3 - can be executed immediately
Points to note when using ROWLOCK in SQL Server
- If you use too many rows by mistake, the database will not be smart enough to automatically upgrade row-level locks to page locks, and the server will consume a lot of memory and CPU due to the overhead of row-level locks until it fails to respond.
- In the select statement, RowLock is meaningless when the combination is not used. The combination With(RowLock,UpdLock) is established. The data queried is locked by RowLock. When the data is updated, the lock will be freed
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues October --https://learn.microsoft.com/en-us/answers/questions/142427/users-always-get-connection-timeout-problem-when-u.html