`
niwtsew
  • 浏览: 71920 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

What is Optimistic Locking vs. Pessimistic Locking

 
阅读更多

What is Optimistic Locking vs. Pessimistic Locking

from http://www.dbasupport.com/forums/archive/index.php/t-7282.html

 

These are methodologies used to handle multi-user issues. How does one handle the fact that 2 people want to update the same record at the same time?

1. Do Nothing
- User 1 reads a record
- User 2 reads the same record
- User 1 updates that record
- User 2 updates the same record
User 2 has now over-written the changes that User 1 made. They are completely gone, as if they never happened. This is called a 'lost update'.

2. Lock the record when it is read. Pessimistic locking
- User 1 reads a record *and locks it* by putting an exclusive lock on the record (FOR UPDATE clause)
- User 2 attempts to read *and lock* the same record, but must now wait behind User 1
- User 1 updates the record (and, of course, commits)
- User 2 can now read the record *with the changes that User 1 made*
- User 2 updates the record complete with the changes from User 1
The lost update problem is solved. The problem with this approach is concurrency. User 1 is locking a record that they might not ever update. User 2 cannot even read the record because they want an exclusive lock when reading as well. This approach requires far too much exclusive locking, and the locks live far too long (often across user control - an *absolute* no-no). This approach is almost *never* implemented.

3. Use Optimistic Locking. Optimistic locking does not use exclusive locks when reading. Instead, a check is made during the update to make sure that the record has not been changed since it was read. This can be done by checking every field in the table.
ie. UPDATE Table1 SET Col2 = x WHERE COL1=:OldCol1 AND COl2=:OldCol AND Col3=:OldCol3 AND...
There are, of course, several disadvantages to this. First, you must have already SELECTed every single column from the table. Secondly, you must build and execute this massive statement. *Most* people implement this, instead, through a single column, usually called timestamp. This column is used *for no other purpose* than implementing optimistic concurrency. It can be a number or a date. The idea is that it is given a value when the row is inserted. Whenever the record is read, the timestamp column is read as well. When an update is performed, the timestamp column is checked. If it has the same value at UPDATE time as it did when it was read, then all is well, the UPDATE is performed and *the timestamp is changed!*. If the timestamp value is different at UPDATE time, then an error is returned to the user - they must re-read the record, re-make their changes, and try to update the record again.

- User 1 reads the record, including the timestamp of 21
- User 2 reads the record, including the timestamp of 21
- User 1 attempts to update the record. The timestamp in had (21) matches the timestamp in the database(21), so the update is performed and the timestamp is update (22).
- User 2 attempts to update the record. The timestamp in hand(21) *does not* match the timestamp in the database(22), so an error is returned. User 2 must now re-read the record, including the new timestamp(22) and User 1's changes, re-apply their changes and re-attempt the update.

分享到:
评论

相关推荐

    Hibernate锁机制_悲观锁和乐观锁

    一、悲观锁(Pessimistic Locking) 悲观锁是一种预防并发访问的机制,Hibernate 通过对数据库的锁定来实现。悲观锁假定任何时刻存取数据时,都可能有另一个客户也正在存取同一笔数据,因此对数据采取了数据库层次...

    Doctrine ORM for PHP.pdf

    What is Doctrine?....................................................................................................13 What is an ORM?....................................................................

    Hibernate Reference Documentation3.1

    11.4. Pessimistic Locking 11.5. Connection Release Modes 12. Interceptors and events 12.1. Interceptors 12.2. Event system 12.3. Hibernate declarative security 13. Batch processing 13.1. Batch inserts...

    Hibernate乐观锁和悲观锁分析

    主要分为两种类型:乐观锁(Optimistic Locking)和悲观锁(Pessimistic Locking)。 **悲观锁(Pessimistic Locking)** 悲观锁假设数据在任何时候都可能发生并发冲突,因此在数据读取时就对其进行锁定,确保在...

    最新hibernate 4.1.8.Final版本(2012年11-01最新发布)

    - **并发控制策略**:提供了更灵活的并发控制策略,如.optimistic-locking(乐观锁)和.pessimistic-locking(悲观锁),可以根据应用需求进行选择。 4. **连接池集成** - Hibernate支持多种连接池实现,如C3P0、...

    hibernate3.04中文文档.chm

    12.4. 悲观锁定(Pessimistic Locking) 13. 拦截器与事件(Interceptors and events) 13.1. 拦截器(Interceptors) 13.2. 事件系统(Event system) 13.3. Hibernate的声明式安全机制 14. 批量处理(Batch ...

    NHibernate参考文档 2.0.0 chm

    10.6. 悲观锁定(Pessimistic Locking) 10.7. 连接释放模式(Connection Release Modes) 11. 拦截器与事件 11.1. 拦截器 11.2. 事件系统 12. HQL: NHibernate查询语言 12.1. 大小写敏感性问题 12.2. from子句 12.3. ...

    EBS FORM开发常用技巧

    12. **并发控制**:了解并应用并发版本控制(Optimistic Locking和Pessimistic Locking),解决多用户同时编辑同一数据的问题。 五、调试与测试 13. **调试技巧**:熟练使用Oracle Forms的调试工具,如Breakpoints...

    NHibernate中文帮组文档(2008.11月更新)

    10.6. 悲观锁定(Pessimistic Locking) 10.7. 连接释放模式(Connection Release Modes) 11. 拦截器与事件 11.1. 拦截器 11.2. 事件系统 12. HQL: NHibernate查询语言 12.1. 大小写敏感性问题 12.2. from子句 12.3. ...

    hibernate 框架详解

    悲观锁定(Pessimistic Locking) 13. 拦截器与事件(Interceptors and events) 13.1. 拦截器(Interceptors) 13.2. 事件系统(Event system) 13.3. Hibernate的声明式安全机制 14. 批量处理(Batch processing)...

    HibernateAPI中文版.chm

    11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate...

    Hibernate 中文 html 帮助文档

    11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的...

    Hibernate中文详细学习文档

    11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate...

    Hibernate教程

    12.4. 悲观锁定(Pessimistic Locking) 13. 拦截器与事件(Interceptors and events) 13.1. 拦截器(Interceptors) 13.2. 事件系统(Event system) 13.3. Hibernate的声明式安全机制 14. 批量处理(Batch processing...

    mysql锁解决并发问题共7页.pdf.zip

    6. **乐观锁(Optimistic Locking)**:不立即锁定数据,而是先进行操作,再在提交时检查期间是否有其他事务修改了数据。InnoDB存储引擎的版本系统就是一种乐观锁实现。 7. **悲观锁(Pessimistic Locking)**:在...

    最全Hibernate 参考文档

    11.4. 悲观锁定(Pessimistic Locking) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明式安全机制 13. 批量处理(Batch processing...

    Hibernate3+中文参考文档

    11.4. 悲观锁定(Pessimistic Locking) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明式安全机制 13. 批量处理(Batch processing...

    孙卫琴hibernate sourcecode6

    此外,还涉及了Hibernate的并发策略,如Pessimistic Write Locking、Pessimistic Read Locking和Optimistic Locking,以及它们在实际应用场景中的选择与优化。 通过这些章节的学习,读者不仅能理解Hibernate如何...

    Hibernate参考文档

    11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的...

    hibernate 体系结构与配置 参考文档(html)

    悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明...

Global site tag (gtag.js) - Google Analytics