“选择更新”当该行不存在时,防止其他连接插入 [英] Does "select for update" prevent other connections inserting when the row is not present

查看:107
本文介绍了“选择更新”当该行不存在时,防止其他连接插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的是选择更新查询是否将锁定不存在的行。

I'm interested in whether a select for update query will lock a non-existent row.

例如

表FooBar有两列,foo和bar,foo有一个唯一的索引

Table FooBar with two columns, foo and bar, foo has a unique index


  • 问题查询从FooBar选择栏,其中foo =? for update

  • 如果查询返回零行


    • 发出查询 insert into FooBar(foo,bar)values(?,?)

    • Issue query select bar from FooBar where foo = ? for update
    • If query returns zero rows
      • Issue query insert into FooBar (foo, bar) values (?, ?)

      现在是否可能插入会导致索引违例或 select for update 阻止?

      Now is it possible that the insert would cause an index violation or does the select for update prevent that?

      对SQLServer(2005/8),Oracle和MySQL的行为感兴趣。

      Interested in behaviour on SQLServer(2005/8), Oracle and MySQL.

      推荐答案

      SELECT ... FOR UPDATE对不存在的行没有影响(语句只是引发一个No Data Found异常)。 INSERT语句将防止重复的唯一/主键值。尝试插入相同键值的任何其他事务将阻塞,直到第一个事务提交(此时被阻止的事务将获得重复的键错误)或回滚(在该时间被阻止的事务继续)。

      In Oracle, the SELECT ... FOR UPDATE has no effect on a non-existent row (the statement simply raises a No Data Found exception). The INSERT statement will prevent a duplicates of unique/primary key values. Any other transactions attempting to insert the same key values will block until the first transaction commits (at which time the blocked transaction will get a duplicate key error) or rolls back (at which time the blocked transaction continues).

      这篇关于“选择更新”当该行不存在时,防止其他连接插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆