Oracle 中写入的乐观锁定
当我执行以下操作时,我注意到:
update t set x = 1 where x = 0; // (1) session 1
update t set x = 2 where x = 0; // (2) session 2
commit; // (3) session 2
在第 (2) 行,会话 2 将等待会话 (1) 提交。
问题是,我可能有很多用户使用该表,但我不希望有人保持会话打开以阻止所有其他用户。
理想情况下,我希望会话 2 提交成功,然后会话 1 在尝试提交时抛出错误,如下所述: 乐观并发控制。
有没有办法让 Oracle 这样做? (如果有影响的话,我正在使用 Oracle 10g)。
基本原理和(可能是糟糕的)解决方案
我有一个夜间复制过程,可能会影响表上的行。我不希望这被在其中一行上打开会话的用户阻止。
我能想到的解决此问题的唯一方法是不向用户提供直接表访问权限,而是创建一个可更新视图或写入临时表的 PL/SQL 函数,然后为用户提供一个执行实际写入和操作的过程。承诺。这样,行被锁定的时间将仅在“提交过程”的执行期间,这将是有限的。
我想要一个类似的解决方案,但最好更简单。
I've noticed when I do the following:
update t set x = 1 where x = 0; // (1) session 1
update t set x = 2 where x = 0; // (2) session 2
commit; // (3) session 2
At line (2), session 2 will wait for session (1) to commit.
The issue is that I may have lots of users using this table, but I don't want one holding the session open to block all other users.
Ideally, I'd like the session 2 commit to succeed, and then session 1 to throw an error when it attempts to commit, as described here: Optimistic concurrency control.
Is there a way to get Oracle to behave in this way? (I'm using Oracle 10g if that makes a difference).
Rationale and (perhaps bad) solution
I have a nightly replication process that can affect rows on the table. I don't want this to be blocked by a user who leaves open a session on one of those rows.
The only way I can think of to work around this to not give users direct table access, but instead create an updatable view or PL/SQL functions that write to a temporary table, and then provide the user a procedure that performs the actual writes and commits. This way, the time that rows will be locked will be only during the execution of the "commit procedure", which would be limited.
I'd like a solution that is something like this but preferably easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论