How to do a safe “SELECT FOR UPDATE” with a WHERE condition over multiple tables on a DB2?

前端 未结 1 1434
我在风中等你
我在风中等你 2021-02-14 00:44

Problem

On a DB2 (version 9.5) the SQL statement

SELECT o.Id FROM Table1 o, Table2 x WHERE [...] FOR UPDATE WITH RR

gives me the erro

相关标签:
1条回答
  • First, for having isolation level READ_COMMITTED you do not need to specify WITH RR, because this results in the isolation level SERIALIZABLE. To specify WITH RS (Read Stability) is enough.

    To propagate the FOR UPDATE WITH RS to the inner select you have to specify additionally USE AND KEEP UPDATE LOCKS.

    So the complete statement looks like this:

    SELECT t.Id FROM Table t WHERE t.Id IN (
        SELECT o.Id FROM Table1 o, Table2 x WHERE [...]
    ) FOR UPDATE WITH RS USE AND KEEP UPDATE LOCKS
    

    I made some tests on a DB2 via JDBC and it worked without deadlocks.

    0 讨论(0)
提交回复
热议问题