Oracle 中的并发更新:锁定与否?
我很困惑。我正在阅读有关 Oracle 中的 MVCC 的内容。我以为MVCC意味着没有锁。但是,我在其他地方读到,无论隔离级别如何,所有 UPDATE 都会自动锁定。有人可以解释一下 Oracle 更新期间会发生什么吗?当多个已提交读事务尝试执行并发更新 t set c = c + 1 where id = 3 时会发生什么。假设在任一事务之前 c = 1,结果是什么?锁和 SCN 发生了什么?
Begin T1
Begin T2
T1: update t set c = c + 1 where id = 3
T2: update t set c = c + 1 where id = 3
Commit T1
Commit T2
I'm confused. I'm reading about MVCC in Oracle. I thought MVCC meant no locks. But, I read somewhere else that all UPDATE
s do automatic locking, regardless of the isolation level. Can someone explain what happens during an Oracle update? And what happens when multiple read committed transactions try to do a concurrent update t set c = c + 1 where id = 3
. What's the result, given c = 1 before either of the transactions, and what's going on with the locks and SCN?
Begin T1
Begin T2
T1: update t set c = c + 1 where id = 3
T2: update t set c = c + 1 where id = 3
Commit T1
Commit T2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,无论隔离级别如何,这都会锁定该行。使用MVCC,您可以在没有锁的情况下获得一致的读取,但在写入时仍然需要锁。
第二个事务将等待第一个事务完成(例如:
COMMIT
或ROLLBACK
),然后再尝试执行任何操作。因此,在这种情况下,T2 上的光标将“挂起”在更新上,等待 T1 完成。T1 提交后您将获得一个新的 SCN,T2 提交后您将获得另一个 SCN。
You're right, this will lock the row regardless of the isolation level. With MVCC you can get consistent reads with no locks, but you still need locks when writing.
The second transaction will wait for the first one to finish (eg:
COMMIT
orROLLBACK
) before attempting to do anything. So in this case the cursor on T2 would "hang" on the update, waiting for T1 to finish.You'll get a new SCN after T1 commits and another after T2 commits.