The pessimistic lock, as its name implies, is very pessimistic. Every time you get the data, you think someone else will modify it. So every time you get the data, you will lock it, in this way, others will block the data until it gets the lock. In traditional relational databases, many of these locks are used, such as row locks, table locks, read locks, and write locks.
Optimistic lock, as its name implies, is optimistic. Every time you get the data, you think that others will not modify it, so they will not lock it, however, during the update process, the system determines whether someone else updates the data during this period. You can use the version number and other mechanisms. Optimistic locks apply to multiple read application types, which can improve throughput,If a database provides a mechanism similar to write_condition, it actually provides optimistic locks.
The two locks have their own advantages and disadvantages and cannot be considered better than the other. For example, optimistic locks apply to the case where there are few writes, that is, when a conflict is rare, this saves the lock overhead and increases the overall throughput of the system. However, if there are frequent conflicts, the upper-layer applications will constantly retry, which reduces the performance. Therefore, it is more appropriate to use pessimistic locks in this case.
Original article: http://blog.csdn.net/hongchangfirst/article/details/26004335
Author:Hongchangfirst
Hongchangfirst home: http://blog.csdn.net/hongchangfirst