Is there a way to BULK COLLECT with FOR UPDATE and not lock ALL the rows?

Currently, we fetch a cursor on a few million rows using BULK COLLECT.
In a FORALL loop, we update the rows.
What is happening now, is that we run this procedure at the same time, and there is another session running a MERGE statement on the same table, and a DEADLOCK is created between them.
I'd like to add to the cursor the FOR UPDATE clause, but from what i've read,
it seems that this will cause ALL the rows in the cursor to become locked.
This is a problem, as the other session is running MERGE statements on the table every few seconds, and I don't want it to fail with ORA-0054 (resource busy).
What I would like to know is if there is a way, that only the rows in the
current bulk will be locked, and all the other rows will be free for updates.
To reproduce this problem:
1. Create test table:
create table TEST_TAB
ID1 VARCHAR2(20),
ID2 VARCHAR2(30),
LAST_MODIFIED DATE
2. Add rows to test table:
insert into TEST_TAB (ID1, ID2, LAST_MODIFIED)
values ('416208000770698', '336015000385349', to_date('15-11-2009 07:14:56', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST_TAB (ID1, ID2, LAST_MODIFIED)
values ('208104922058401', '336015000385349', to_date('15-11-2009 07:11:15', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST_TAB (ID1, ID2, LAST_MODIFIED)
values ('208104000385349', '336015000385349', to_date('15-11-2009 07:15:13', 'dd-mm-yyyy hh24:mi:ss'));
3. Create test procedure:
CREATE OR REPLACE PROCEDURE TEST_PROC IS
TYPE id1_typ is table of TEST_TAB.ID1%TYPE;
TYPE id2_typ is table of TEST_TAB.ID2%TYPE;
id1_arr id1_typ;
id2_arr id2_typ;
CURSOR My_Crs IS
SELECT ID1, ID2
FROM TEST_TAB
WHERE ID2 = '336015000385349'
FOR UPDATE;
BEGIN
OPEN My_Crs;
LOOP
FETCH My_Crs bulk collect
INTO id1_arr, id2_arr LIMIT 1;
Forall i in 1 .. id1_arr.COUNT
UPDATE TEST_TAB
SET LAST_MODIFIED = SYSDATE
where ID2 = id2_arr(i)
and ID1 = id1_arr(i);
dbms_lock.sleep(15);
EXIT WHEN My_Crs%NOTFOUND;
END LOOP;
CLOSE My_Crs;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000,
'Test Update ' || SQLCODE || ' ' || SQLERRM);
END TEST_PROC;
4. Create another procedure to check if table rows are locked:
create or replace procedure check_record_locked(p_id in TEST_TAB.ID1%type) is
cursor c is
select 'dummy'
from TEST_TAB
WHERE ID2 = '336015000385349'
and ID1 = p_id
for update nowait;
e_resource_busy exception;
pragma exception_init(e_resource_busy, -54);
begin
open c;
close c;
dbms_output.put_line('Record ' || to_char(p_id) || ' is not locked.');
rollback;
exception
when e_resource_busy then
dbms_output.put_line('Record ' || to_char(p_id) || ' is locked.');
end check_record_locked;
5. in one session, run the procedure TEST_PROC.
6. While it's running, in another session, run this block:
begin
check_record_locked('208104922058401');
check_record_locked('416208000770698');
check_record_locked('208104000385349');
end;
7. you will see that all records are identified as locked.
Is there a way that only 1 row will be locked, and the other 2 will be unlocked?
Thanks,
Yoni.

I don't have database access on weekends (look at it as a template)
suppose you
create table help_iot
(bucket number,
id1    varchar2(20),
constraint help_iot_pk primary key (bucket,id1)
organization index;not very sure about the create table syntax above.
declare
  maximal_bucket number := 10000; -- will update few hundred rows at a time if you must update few million rows
  the_sysdate date := sysdate;
begin
  truncate table help_iot;
  insert into help_iot
  select ntile(maximal_bucket) over (order by id1) bucket,id1
    from test_tab
   where id2 = '336015000385349';
  for i in 1 .. maximal_bucket
  loop
    select id1,id2,last_modified
      from test_tab
     where id2 = '336015000385349'
       and id1 in (select id1
                     from help_iot
                    where bucket = i
       for update of last_modified;
    update test_tab
       set last_modified = the_sysdate
     where id2 = '336015000385349'
       and id1 in (select id1
                     from help_iot
                    where bucket = i
    commit;
    dbms_lock.sleep(15);
  end loop;
end;Regards
Etbin
introduced the_sysdate if last_modified must be the same for all updated rows
Edited by: Etbin on 29.11.2009 16:48

Similar Messages

  • Is there a way to integrate iCal with both reminder and note?  It would be useful to be able to "date" tag both these apps with iCal, so you can augment or even replace an integrated planner

    Is there a way to integrate Reminder and Note into iCal?  It would be useful to be able to "Date" tag notes and reminders into the iCal display--this would go a long way to augmenting and replacing a paper planner.

    No way in iCal!
    The calendar app of my old MassagePad called Apple Newton was able to calculate the duration of an event. That was in the year 1998.
    We're now living in the year 2014 - 16 years later - more then 10 years after OS 10.0, but we still have no calendar app that can do that simple thing.
    Thanks Apple for a another embarrassing failure!

  • Serious problem with xorg - keyboard and mouse locks all the time.

    In the last week I've notices serious problems with xorg.
    After some event, I am not sure which one, keyboard stops responding.
    Mouse cursor works, but I can't click on anything.
    I can't switch to text console.
    But I can kill xorg with CTRL+ALT+BACKSPACE.
    Have you noticed something similiar?
    I use xfce4, [testing] repository.

    Jacek Poplawski wrote:
    In the last week I've notices serious problems with xorg.
    After some event, I am not sure which one, keyboard stops responding.
    Mouse cursor works, but I can't click on anything.
    I can't switch to text console.
    But I can kill xorg with CTRL+ALT+BACKSPACE.
    Have you noticed something similiar?
    I use xfce4, [testing] repository.
    I had that a few days ago, couldn't reproduce it. I was also running xfce4. I havn't had it today and im running xfdesktop+xfce-mcs-manager+pekwm+xfce4-panel... so it may have been xfwm4.
    James

  • I have collected alot of music over the years and the new update will delete all my music.  I have puchased a new computer about a year ago so the previous music that I have is not available.  Is there anyway to get the update and not lose all my music

    I have collected alot of music over the past couple years and the new update for the itouch says that it will erase all music and videos on the ipod.  Is there anyway to do the update and not lose all my music or is there a way to take all the music off my ipod and store it on my computer to put back on after the update?  Any help

    ... I have had numerous computers over the past years
    Let me guess, you have had numerous Windows computers, all of which crashed or became worthless virus-ridden junk. Happens every day, yet people think Macs are expensive.
    Anyway, you are going to need the assistance of third party utilities to extract your iPod Touch's content. See wjosten's procedure here:
    https://discussions.apple.com/docs/DOC-3141

  • Is there a iMovie HD that works with intel processors but still has all the conversion features like iMovie Hd 5.02.(111)

    is there a iMovie HD that works with intel processors but still has all the conversion features like iMovie Hd 5.02.(111)

    iMovie version 6.

  • Cannon 8800F not working with OS X yosemite 10.10.1  . I have downloaded the latest drivers and firmware from Canon. I called Canon for support and went through all the checks , but, still my iMac did not see the scanner. Any new ideas?

    cannon 8800F not working with OS X yosemite 10.10.1  . I have downloaded the latest drivers and firmware from Canon. I called Canon for support and went through all the checks , but, still my iMac did not see the scanner. Any new ideas?

    Have you done any of the following?
    http://support.apple.com/kb/PH18614 OS X Yosemite: Reset the printing system
    Repaired permissions & restarted your comp after the installations of the drivers?
    Sometimes, installing the GIMP drivers help with printing issues: http://gimp-print.sourceforge.net/MacOSX.php
    What to do when you can't print
    =============
    not working with OS X yosemite 10.10.1
    Per your profile
    Mac OS X (10.7.3)
    Conflicting info:  Please update/correct your profile so that you can receive the correct troubleshooting suggestions.  This will assist the users in trying to help you.  Thank you.

  • HT3986 i formated my macbook pro with windows 7 and i lost all the drivers, does anyone know how to recover the drivers?

    i formated my macbook pro with windows 7 and i lost all the drivers, does anyone know how to recover the drivers?

    How did you get them to install the first time and where did you get them?
    Read the Installation Guide, Boot Camp 4.0 FAQ and the Windows 7 FAQ.
    Here's the Boot Camp Manual.

  • TS1424 2 songs were cut off and not downloaded all the way, what can i do

    2 songs were cut off and not downloaded all the way, what can i do

    If your country's iTunes Store allows you to redownload purchased tracks, I'd delete your current copies of the dodgy tracks and try redownloading fresh copies. For instructions, see the following document:
    Downloading past purchases from the App Store, iBookstore, and iTunes Store
    Otherwise, I'd report the problem to the iTunes Store.
    Log in to the Store. Click on "Account" in your Quick Links. When you're in your Account information screen, go down to Purchase History and click "See all".
    Find the items that are not playing properly. If you can't see "Report a Problem" next to the items, click the "Report a problem" button. Now click the "Report a Problem" links next to the items.

  • Is there a way to link Words with Friends on my mini iPad to the Mac Pro Computer?

    Is there a way to link the game Words with Friends from a mini iPad to a Mac Pro Computer?

    You need an Exchange or IMAP account to do what you want. This will keep mail in sync on all of your devices.

  • I have switched computers - old one stolen - and lost access to my 800 or so song library. I started a new library. Is there any way I can retrieve my old library and add it to the new one?

    I am not very tech oriented - I love my Ipod - had a library of 800- 900 songs - my computer was stolen, I have since bought a new one - started a new library because I could not figure out how to access my old library. Is there any way I can somehow find that original library and add it to my new library??

    If these are photos that were synced to your phone, the only way to extract them from your phone is third-party software, like this:
    http://www.wideanglesoftware.com/touchcopy/index.php

  • Is there any way to track an itouch if it's not connected to the internet?

    I lost my ipod but I can't use the app Track My Ipod as my itouch is not connected to the internet. Is there any way I'm able to find it?

    you will have to use the old fashioned way, like if you lost a wallet or purse.

  • Is there a way to delete emails on my oPhone and not my PC Inbox?

    Is there a way to delete the emails on my iPhone without deleting them on my PC inbox?

    Blackberry email works very differently to iPhone email. All email on Blackberry devices goes through RIMs servers so it can be deleted independently of the mail on the original server.
    The iPhone talks directly to the email accounts original server like a computer email client does, so you're manipulating mail directly on the server without going through an intermediary. Therefore whatever actions you do on your iPhone are actually done directly on the mail server.

  • Is there a way to rename PDF form fields without worrying about having all the right fonts?

    I'm trying to merge data into an IRS form... for some reason, they duplicated the names for a few fields which causes a problem with merging.
    I've installed Acrobat X and tried to edit the form in LiveCycle, but I don't have all the fonts, so I can't do it correctly without altering the IRS form.
    Can someone point to a right tool to do a simple rename of a few form field names?
    Thanks in advance!

    The field names don't have anything to do with fonts, but you might be better off asking in the LiveCycle Designer forum.

  • I seem to have a lot of Apple IDs. Is there a way to get a list of them and kill them all off until there is just one left?

    I can't get on the icloud because my Apple ID is a birthday, password and email that are not mine. How do I fix this?

    Welcome to the Apple community.
    If you mean that Find My Phone is asking for a password to a different Apple ID to your current Apple ID.
    This feature has been introduced to make stolen phones useless to those that have stolen them.
    However it can also arise when the user has changed their Apple ID details with Apple and not made the same changes to their iCloud account/Find My Phone on their device before upgrading to iOS 7, or if you restore from a previous back up made before you changed your details.
    The only solution is to change your Apple ID back to its previous state with Apple at My Apple ID using your current password, you don’t need access to this address if it’s previously been used with your Apple ID, once you have saved these details enter the password as requested on your device and then turn off "find my phone" and delete the account from your device.
    You should then change your Apple ID back to its current state, save it once again and then log back in using your current Apple ID. Finally, turn "find my phone" back on once again.
    This article provides more information about Activation Lock.

  • Are there any update or patches for Firefox 4. It works very poorly with Windowa 7 and not at all with Youtube?

    It freezes up all the time..."Firefox not responding". Also it will mot allow me to share YouTube videos and Facebook. i have none of these problems with IE or the old Firefox 3.

    An update was released today. Give it a spin and see whether it helps! (Currently, I believe you would have to download the full install, but hopefully the update button will start working in a few hours.)
    For what it's worth, I haven't noticed any problems running Firefox 4 on Windows 7, but I have not tried posting anything to YouTube or Facebook.

Maybe you are looking for

  • Can we hide the lines between the columns and rows of an alv in wd abap

    HI all ,   I know that we can colour cell/column/row in an alv in wd abap.    but, can we hide the lines between the columns and rows of an alv in wd abap.      i have checked this link [hiding lines b/n rows and columns of an  alv|http://help.sap.co

  • Data transfer position for candidate

    Hi Experts, We are implementing ECC 6, E Recruiting. When we do data transfer activity for a candidate then we want to pull the position on which data transfer activity was done for candidate in a Z Report. Can any one tell me in which info type posi

  • Threading on different servers

    Hi, I have a single client that sends same data parameters to two servers. It needs each server to complete a few runs and report the results. At the same time the client has an overall target to accomplish, say 200 runs. Servers may execute a single

  • Itunes - grey dotted circle - HELP

    Hello guys, i have a problem that grey dotted circle appears next to my music in library. It is whole grey and when I sync it nothing syncs and I cant play the music on my iPhone! Help please!

  • Hi, errors with raw type in export

    how export data type raw? thanks