简体   繁体   中英

DB2 “WITH (HOLDLOCK)” equivalent

In SQL Server you I can write the following:

UPDATE myTable WITH (HOLDLOCK) SET myIntColumn = 123

I need to port this statement to DB2. What would be the equivalent of "WITH (HOLDLOCK)"?

The equivalent statement in DB2 is simply,

UPDATE myTable SET myIntColumn = 123

DB2 will hold locks on the rows affected by an UPDATE statement until the transaction is committed.

A few notes:

1) If you haven't set autoCommit off in your application, the transaction will be implicitly committed as soon as this statement finishes executing. Generally autoCommit is enabled by default.

2) DB2 9.7 added Currently Committed locking semantics, which allows other applications to read committed data even when rows are locked but not yet committed (this is similar to how Oracle uses rollback segments). If you're running on DB2 9.7 and are worried about other applications reading the "old" version of the rows you're updating, you may need to consider this. Documentation for Currently Committed Semantics

3) If you are reading a cursor and selectively updating rows in the result set (ie UPDATE myTable ST myIntColumn = 123 WHERE CURRENT OF cursor ), and you want DB2 to hold locks on the other rows in the result set that have not been updated, then you need to look at using a different statement isolation level (ie read stability or repeatable read).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM