- 浏览: 71920 次
- 性别:
- 来自: 北京
文章分类
最新评论
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.
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 587well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 398http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 660this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 745http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 695http://archive.plugins.jquery.c ... -
what is DAO
2011-04-15 13:42 767http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 708I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 665http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 713http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1675http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 832Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 901The XML declaration typically a ... -
UCM
2010-05-08 11:41 745Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 762refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 843As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 935copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 816copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 866It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1308copy from http://blog.cornetdes ... -
communicate between plug-ins in different sandboxes
2009-12-17 21:17 681Given that there are many cases ...
相关推荐
一、悲观锁(Pessimistic Locking) 悲观锁是一种预防并发访问的机制,Hibernate 通过对数据库的锁定来实现。悲观锁假定任何时刻存取数据时,都可能有另一个客户也正在存取同一笔数据,因此对数据采取了数据库层次...
What is Doctrine?....................................................................................................13 What is an ORM?....................................................................
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...
主要分为两种类型:乐观锁(Optimistic Locking)和悲观锁(Pessimistic Locking)。 **悲观锁(Pessimistic Locking)** 悲观锁假设数据在任何时候都可能发生并发冲突,因此在数据读取时就对其进行锁定,确保在...
- **并发控制策略**:提供了更灵活的并发控制策略,如.optimistic-locking(乐观锁)和.pessimistic-locking(悲观锁),可以根据应用需求进行选择。 4. **连接池集成** - Hibernate支持多种连接池实现,如C3P0、...
12.4. 悲观锁定(Pessimistic Locking) 13. 拦截器与事件(Interceptors and events) 13.1. 拦截器(Interceptors) 13.2. 事件系统(Event system) 13.3. Hibernate的声明式安全机制 14. 批量处理(Batch ...
10.6. 悲观锁定(Pessimistic Locking) 10.7. 连接释放模式(Connection Release Modes) 11. 拦截器与事件 11.1. 拦截器 11.2. 事件系统 12. HQL: NHibernate查询语言 12.1. 大小写敏感性问题 12.2. from子句 12.3. ...
12. **并发控制**:了解并应用并发版本控制(Optimistic Locking和Pessimistic Locking),解决多用户同时编辑同一数据的问题。 五、调试与测试 13. **调试技巧**:熟练使用Oracle Forms的调试工具,如Breakpoints...
10.6. 悲观锁定(Pessimistic Locking) 10.7. 连接释放模式(Connection Release Modes) 11. 拦截器与事件 11.1. 拦截器 11.2. 事件系统 12. HQL: NHibernate查询语言 12.1. 大小写敏感性问题 12.2. from子句 12.3. ...
悲观锁定(Pessimistic Locking) 13. 拦截器与事件(Interceptors and events) 13.1. 拦截器(Interceptors) 13.2. 事件系统(Event system) 13.3. Hibernate的声明式安全机制 14. 批量处理(Batch processing)...
11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate...
11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的...
11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate...
12.4. 悲观锁定(Pessimistic Locking) 13. 拦截器与事件(Interceptors and events) 13.1. 拦截器(Interceptors) 13.2. 事件系统(Event system) 13.3. Hibernate的声明式安全机制 14. 批量处理(Batch processing...
6. **乐观锁(Optimistic Locking)**:不立即锁定数据,而是先进行操作,再在提交时检查期间是否有其他事务修改了数据。InnoDB存储引擎的版本系统就是一种乐观锁实现。 7. **悲观锁(Pessimistic Locking)**:在...
11.4. 悲观锁定(Pessimistic Locking) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明式安全机制 13. 批量处理(Batch processing...
11.4. 悲观锁定(Pessimistic Locking) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明式安全机制 13. 批量处理(Batch processing...
此外,还涉及了Hibernate的并发策略,如Pessimistic Write Locking、Pessimistic Read Locking和Optimistic Locking,以及它们在实际应用场景中的选择与优化。 通过这些章节的学习,读者不仅能理解Hibernate如何...
11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的...
悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明...