Have “select for update” block on nonrexisting rows

前端 未结 3 609
深忆病人
深忆病人 2021-02-07 03:24

we have some persistent data in an application, that is queried from a server and then stored in a database so we can keep track of additional information. Because we do not wan

3条回答
  •  难免孤独
    2021-02-07 03:28

    Looking at the code added in the second edit, it looks right.

    As for it looking like a hack, there's a couple options - basically it's all about moving the database logic to the database.

    One is simply to put the whole select for update, if not exist then insert logic in a function, and do select get_object(key1,key2,etc) instead.

    Alternatively, you could make an insert trigger that will ignore attempts to add an entry if it already exists, and simply do an insert before you do the select for update. This does have more potential to interfere with other code already in place, though.

    (If I remember to, I'll edit and add example code later on when I'm in a position to check what I'm doing.)

提交回复
热议问题