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 error message SQLSTATE=42829
(The FOR UPDATE clause is not allowed because the table specified by the cursor cannot be modified).
I need to specify WITH RR
, because I'm running on isolation level READ_COMMITTED
, but I need my query to block while there is another process running the same query.
If I instead query like this:
SELECT t.Id FROM Table t WHERE t.Id IN (
SELECT o.Id FROM Table1 o, Table2 x WHERE [...]
) FOR UPDATE WITH RR
everything works fine.
But now I occasionally get deadlock exceptions when multiple processes perform this query simultaneously.
Is there a way to formulate the FOR UPDATE
query without introducing a place where a deadlock can occur?
The subquery defines an internal query that can be used inside a SELECT, INSERT, UPDATE and DELETE statement. It is a straightforward method to update the existing table data from other tables. The above query uses a SELECT statement in the SET clause of the UPDATE statement.
The SELECT FOR UPDATE statement is used to order transactions by controlling concurrent access to one or more rows of a table. It works by locking the rows returned by a selection query, such that other transactions trying to access those rows are forced to wait for the transaction that locked the rows to finish.
UPDATE queries can change all tables' rows, or we can limit the update statement affects for certain rows with the help of the WHERE clause. Mostly, we use constant values to change the data, such as the following structures. The full update statement is used to change the whole table data with the same value.
The FOR UPDATE keywords notify the database server that updating is possible, causing it to use more stringent locking than it would with a Select cursor. You cannot modify data through a cursor without this clause. You can specify which columns can be updated.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With