Optimistic vs. Pessimistic locking

前端 未结 10 1202
庸人自扰
庸人自扰 2020-11-21 22:35

I understand the differences between optimistic and pessimistic locking. Now could someone explain to me when I would use either one in general?

And does the answer

10条回答
  •  悲&欢浪女
    2020-11-21 23:13

    Optimistic locking is used when you don't expect many collisions. It costs less to do a normal operation but if the collision DOES occur you would pay a higher price to resolve it as the transaction is aborted.

    Pessimistic locking is used when a collision is anticipated. The transactions which would violate synchronization are simply blocked.

    To select proper locking mechanism you have to estimate the amount of reads and writes and plan accordingly.

提交回复
热议问题