问题
We work with DB2 effective version 8 (more or less, so no CUR_COMMIT) on z/OS.
In our (Java based, though this should not be relevant) application a method exists which runs in a transaction and deletes multiple records from a table called, say, MY_TABLE, based on the value of a certain column which we will call SPECIAL_COLUMN, executing the statement
DELETE FROM MY_TABLE WHERE SPECIAL_COLUMN=?
Apart from executing this statement, some other SQL statements get executed which I omit because for the moment I think they are perhaps not relevant for the problem I describe.
Running the method concurrently we sometimes see the exception
nested exception is com.ibm.db2.jcc.am.SqlException:
UNSUCCESSFUL EXECUTION CAUSED BY DEADLOCK OR TIMEOUT. REASON CODE 00C90088, TYPE OF RESOURCE 00000302, AND RESOURCE NAME ... SQLCODE=-913, SQLSTATE=57033, DRIVER=3.63.131
thrown during the execution of the DELETE FROM MY_TABLE WHERE SPECIAL_COLUMN=? statement. According to http://www.idug.org/p/fo/et/thread=20542 this seems to be related to locks placed on "pages".
My questions are the following:
Can in fact two DELETE statements executed concurrently for the same value of SPECIAL_COLUMN, to which multiple rows correspond, cause such deadlock (a scenario which I have in mind is something like the following: the first statement "puts a lock" on "1st page", second statement "puts a lock" on "2nd page", and then first statement waits for the lock on "2nd page", while the second statement waits for the lock on "1st page". Or is the placing of such locks is "atomic", meaning that if first statement has started to put locks, the 2nd will wait?
Same question for different values of SPECIAL_COLUMN (seems more likely)
In the case such scenarios are possible and might be the reason for the deadlock observed (otherwise we'll have to examine the "unsuspicios" so far SQL), which might be a reasonable solution? (I have thought on synchronizing the Java code, but I think it is not such a good idea; I have thought also on issuing SELECT FOR UPDATE on the rows to be deleted before doing delete, but since additional locks will be involved, I am quite in doubt also about that).
EDIT:
link to a report on a similar problem http://www.dbforums.com/showthread.php?575408-db2-OS390-TABLE-LOCK-DURING-DELETE
来源:https://stackoverflow.com/questions/43082000/can-one-delete-statement-which-deletes-multiple-rows-cause-deadlock