@hfsitumo2001
The real thing is, we generally do not need to worry about records being changed by other users while we are editing them. In general if you read a record, it doesn't matter what state the record is, as the timing of the read is of little consequence. If we read the record either before or after the record was edited, how would we know, and why would it matter? If we did a process at 9:55 am, and someone changed the record, so that doing the process at 10:05am would have produced a different result, in general the time we do our action should not make any difference to us. I mean that what we do is being handled correctly at 9:55am and also handled correctly at 10:05am. We may get a different result. An order may be accepted at 9:55am. By 10:05am the items may be out of stock, or the customer may have exceeded his credit limit, so the order may be rejected at 10:05am, but both response are correct.
A system often used in retail shopping carts is to let any shopper put an item in the cart, but then the availability is rechecked at the payment stage. So the store has 5 "widget" items in stock, and 10 people have them in their shopping carts. When the sixth person tries to check out, the payment system advises him there are none left, and removes the "widget" from his cart. This is, if you will, optimistic locking.
A different system is often used when booking theatre or plane tickets. We get offered a choice of seat at 9:55am. At 10:00am someone who had reserved 2 seats releases those seats. If we try again at 10:05am we would have been offered those 2 extra seats, but this doesn't affect the integrity of the system. We get the seats we reserved allocated to us for a limited period of time while we complete the checkout. This is pessimistic locking.
Either way works, and it's a matter of taste as to which system gives the best or the desired experience for the shopper and the store.
Now, the main thing is how the system achieves integrity to do either of the above processes, to make sure that the correct number of sales are achieved. (
which is done behind the scenes) The pessimistic locking needs to actually prevent multiple users grabbing the same seats by locking them while the active user achieves the reservation. The optimistic system doesn't bother with this. It assumes there won't be a problem, but just before you pay for the goods (
again behind the scenes) it rechecks to see if your original request is still valid. If it's not, it warns you that the details have changed, and it can't complete your transaction.
This is why pessimistic locking needs to be very carefully considered. You don't want to lock records and leave them locked forever, as your system won't work correctly. You need to time the locks so that you maintain them for as short a time as possible. Just long enough to deal with the checkout process. You also need a way to reverse the process if the lock cannot be obtained. You can get a situation where you secure Item 1, and are trying to lock Item 2, but someone else has locked item 2, and is trying to secure item 1. Neither of you can make progress, and the entire system can grind to a halt. This is what
@The_Doc_Man was discussing earlier.
Some of the locking is managed automatically by the system (the database manager software), but some might need to be added by the programmer. So you generally don't want pessimistic locking at all unless you have a real special need for this approach, as you then need to add a lot of defensive code. Conversely and surprisingly with optimistic locking, the system can do the tidying up for you. It tries to repeat your original request at the final stage, and if it cannot be done now, it CAN tell you that the data has changed and it now can't fulfil your request.
There are still some technical points, but general speaking optimistic locking is sufficient, and that is why it is the default.