I have the following simple scenario.
Three computers will be updating a cinema booking table. Should I lock th开发者_StackOverflowe whole table or lock the rows corresponding to seats that will be updates? Can I consider optimistic concurrency control here?
I was just searching about optimistic and pessimistic concurrency control and stumbled about different locks and was wondering which would be the most suitable for which scenarios.
Your DBMS should take care of such locking, you just need to ensure you don't try to update a seat that has already been booked - e.g.
update seat
set status = 'BOOKED', booking_ref = :booking_ref
where seat_no = :seat_no -- identify the seat to book
and status = 'FREE'; -- ensure it is currently free
If 2 session try to book the same seat at the same time, one will succeed and the other will fail. Your code needs to check whether it succeeded or not.
Optimistic vs. pessimistic locking is an application rather than DBMS issue. Rather than go into it all here I'll refer you to an existing SO question.
精彩评论