How are locking mechanisms (Pessimistic/Optimistic) related to database transaction isolation levels?

后端 未结 2 517
悲&欢浪女
悲&欢浪女 2021-01-31 19:37

I am writing a web application where two different users can update a list of things, to do list, for example. I have come to realize that, optimistic locking mechanism works be

2条回答
  •  隐瞒了意图╮
    2021-01-31 20:14

    Locking mechanisms are usually used to implement transaction isolation levels. So transaction isolation levels define contract how your transactions have to behave in concurrent execution. Locking mechanisms are implementation details.

    From application writing perspective you should focus on setting appropriate transaction isolation level. Of course setting specific isolation level implicates locking, but as long as you don't have your application under heavy load, you don't need to take care of it much.

    Import thing is that locking mechanisms differ between database engines. If you write application for one database and after some time you would change db engine, your application may behave differently or some part of it may require rewriting.

    My advice from fifteen years of business application development is not to rely on explicit locking.

提交回复
热议问题