bulk collect using “for update”

后端 未结 2 2068
遇见更好的自我
遇见更好的自我 2021-01-06 13:32

I run into an interesting and unexpected issue when processing records in Oracle (11g) using BULK COLLECT.

The following code was running great, processing through

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 14:35

    The problem is that you're trying to do a fetch across a commit.

    When you open My_Data_Cur with the for update clause, Oracle has to lock every row in the My_Data_1 table before it can return any rows. When you commit, Oracle has to release all those locks (the locks Oracle creates do not span transactions). Since the cursor no longer has the locks that you requested, Oracle has to close the cursor since it can no longer satisfy the for update clause. The second fetch, therefore, must return 0 rows.

    The most logical approach would almost always be to remove the commit and do the entire thing in a single transaction. If you really, really, really need separate transactions, you would need to open and close the cursor for every iteration of the loop. Most likely, you'd want to do something to restrict the cursor to only return 100 rows every time it is opened (i.e. a rownum <= 100 clause) so that you wouldn't incur the expense of visiting every row to place the lock and then every row other than the 100 that you processed and deleted to release the lock every time through the loop.

提交回复
热议问题