当使用乐观锁定策略时,可以解决如下并发问题:
| the first transaction started |
| |
| select a row |
| | the second transaction started
| update the row with version checking |
| | select the same row
| commit txn |
| | update the row with version checking
| |
| | rolls back because version is dirty
但是,如果在极少数情况下,如果第二个事务中的更新是在第一个事务中的更新之后但在事务提交之前怎么办?
| the first transaction started |
| | the second transaction started
| select a row |
| | select the same row
| update the row with version checking |
| | update the row with version checking
| commit txn |
| | rolls back because version is dirty // will it?
| |
| |
我做了一个实验,第二个事务中的更新无法读取“脏”版本,因为第一个事务尚未提交。这种情况下第二笔交易会失败吗?
您在问题中没有说明您实际使用的数据库系统,所以我不知道您系统的详细信息。
但无论如何,在乐观锁定系统下,进程在执行更新语句时不能只检查行版本,因为正是您担心的问题。
对于完全可序列化、隔离的事务,每个进程必须在提交时自动检查其检查和修改的所有行的行版本。因此,在第二种情况下,右侧进程在尝试提交之前不会检测到冲突(您没有为右侧进程包含此步骤)。当它尝试提交时,它会检测到冲突并回滚。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)