ORACLE PL/SQL:在 Oracle SQL Developer 中测试 SELECT FOR UPDATE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4673696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ORACLE PL/SQL: Testing SELECT FOR UPDATE in Oracle SQL Developer
提问by jlrolin
We are currently trying to implement a SELECT FOR UPDATE in order to lock rows. If I run:
我们目前正在尝试实现 SELECT FOR UPDATE 以锁定行。如果我运行:
SELECT * FROM data where rowid = 'AAAQA5AAGAACNbEAA1' FOR UPDATE;
SELECT * FROM data where rowid = 'AAAQA5AAGAACNbEAA1' FOR UPDATE NOWAIT;
I get nothing back in ORACLE SQL Developer. Isn't this supposed to throw me an error? Does this have something to do with using the same user id?
我在 ORACLE SQL Developer 中一无所获。这不是应该给我一个错误吗?这与使用相同的用户 ID 有关系吗?
回答by Justin Cave
What do you mean by "I get nothing back"? Do you mean that neither query returns any rows? If so, that implies that there are no rows in the table that have that ROWID. Do you mean that the first (or second) statement never returns? If so, the session is blocked waiting for the lock to be acquired.
“我一无所获”是什么意思?你的意思是两个查询都没有返回任何行吗?如果是这样,这意味着表中没有具有该 ROWID 的行。你的意思是第一个(或第二个)语句永远不会返回?如果是这样,会话将被阻塞,等待获取锁。
Locks are held by sessions. If the two statements are run in the same session, the second statement will succeed because the the first statement already acquired the lock. If the second statement is run in a different session that happens to be opened by the same user, you should generate an exception that the row is already locked by another session.
锁由会话持有。如果两条语句在同一个会话中运行,第二条语句会成功,因为第一条语句已经获得了锁。如果第二条语句在碰巧由同一用户打开的不同会话中运行,则应生成该行已被另一个会话锁定的异常。
回答by a_horse_with_no_name
You request a lock on a table that you have already locked in thattransaction.
您请求锁定已在该事务中锁定的表。
You need to run the second SELECT from a different connection (i.e. a different transaction)
您需要从不同的连接(即不同的事务)运行第二个 SELECT