What should be the correct/better approach for locking a record in table?
Actually, the prob开发者_运维技巧lem is like this:-
i am having a table, say Payments. I have provided few GUI for searching, viewing and editing the record. Now if the user is trying toTraditional approach of doing this is, have a column in the table, say _isLocked_. and whenever user is performing that action it should change the value of said column to, say, true. Once the process is completed it should reset the value of the column. Also, just before someone tries to perform the action, it should check the value of the column and notifies the user if the record is locked.
What other approached are there to do the same?
SELECT FOR UPDATE is the way. We get this kinda behaviour by using this.
As Vinegar said, SELECT FOR UPDATE
- accept his answer, not mine :)
Make sure to know when the user really wants to edit a row, and when he/she is done. Depending on your application, you could open a separate window where user does the modification and then confirms/cancels, so you can do your COMMIT/ROLLBACK.
And make sure that another user who attempts to change the same row will not face a frozen application and have to wait for the lock to be released.
精彩评论