Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 807067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:20:47+00:00 2026-05-15T00:20:47+00:00

Will the following code lead to a deadlock or should it work without any

  • 0

Will the following code lead to a deadlock or should it work without any problem? I’ve got something similar and it’s working but I didn’t think it would. I thought the parent procedure’s lock would have resulted in a deadlock for the child procedure but it doesn’t seem to be.

If it works, why? My guess is that the nested FOR UPDATE is not running into a deadlock because it’s smart enough to realize that it is being called by the same procedure that has the current lock.

Would this be a deadlock if FOO_PROC was not a nested procedure?

DECLARE
  FOO_PROC(c_someName VARCHAR2) as
    cursor c1 is select * from awesome_people where person_name = c_someName FOR UPDATE;
  BEGIN
    open c1;
    update awesome_people set person_name = UPPER(person_name);
    close c1;
  END FOO_PROC;

  cursor my_cur is select * from awesome_people where person_name = 'John Doe' FOR UPDATE;
BEGIN
  for onerow in c1 loop
    FOO_PROC(onerow.person_name);
  end loop;
END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:20:48+00:00Added an answer on May 15, 2026 at 12:20 am

    It won’t cause a deadlock. THat can only happen when two sessions update the same row because they are employing an optimistic locking strategy. Here is what happens

    Some test data:

     SQL> select * from t23
       2  /
    
     PERSON_NAME
     -----------------------------------------------------------------------------
     Fox in socks
     Mr Knox
     Sam-I-Am
     The Lorax
     John Doe
    
     SQL>
    

    This is your anonymous (with corrected sybtax):.

     SQL> declare
       2      cursor c_jd is
       3          select *
       4          from t23
       5          where person_name = 'John Doe'
       6          for update of person_name;
       7      procedure foo_proc
       8          ( p_name in t23.person_name%type)
       9      is
      10          cursor c_fp is
      11              select *
      12              from t23
      13              where person_name = p_name
      14              for update of person_name;
      15          r_fp c_fp%rowtype;
      16      begin
      17          open c_fp;
      18          fetch c_fp into r_fp;
      19          update t23
      20          set person_name = upper(r_fp.person_name)
      21          where current of c_fp;
      22          close c_fp;
      23      end foo_proc;
      24  begin
      25      for onerow in c_jd loop
      26          foo_proc(onerow.person_name);
      27      end loop;
      28  end;
      29  /
    
     PL/SQL procedure successfully completed.
    
     SQL>
    

    And this is the outcome

    SQL> select * from t23
    2 /

     PERSON_NAME
     -----------------------------------------------------------------------------
     Fox in socks
     Mr Knox
     Sam-I-Am
     The Lorax
     JOHN DOE
    
    SQL>
    

    So does it succeed? Because the FOR UPDATE is a session level lock. The two locks are issued from the same session so Oracle is smart enough to resolve them without contention. Howver if you were to do something like declare an PRAGMA AUTONOMOUS_TRANSACTION in FOO_PROC() it would hurl

    ORA-00060: deadlock detected while waiting for resource
    

    The fact that two calls to FOR UPDATE in the same session do not not fail in this manner is an important piece of architectural design. It is not possible to tell whether a procedure issues a lock without looking at the source code. So when PROC_A() calls PROC_B() it has no idea whether that procedure issues a lock. But PROC_A() can issues its own lock, confident that this action will not cause PROC_B() to fail. This is a good thing, because it upholds the Law of Demeter and reduces coupling.

    Of course, your scenario is artificial, and would be rejected as bad practice in a code review, but that is a different issue!

    edit

    “To test this I did make FOO_PROC
    autonomous and it did not run into a
    deadlock; is that because it’s in the
    same session?”

    Are you sure? The AUTONOMOUS_TRANSACTION pragma means precisely that FOO_PROC() runs in its own discrete session, and so fails to get a lock:

    SQL> declare
      2      cursor c_jd is
      3          select *
      4          from t23
      5          for update of person_name;
      6      procedure foo_proc
      7          ( p_name in t23.person_name%type)
      8      is
      9          pragma autonomous_transaction;
     10          cursor c_fp is
     11              select *
     12              from t23
     13              where person_name = p_name
     14              for update of person_name;
     15          r_fp c_fp%rowtype;
     16      begin
     17          dbms_output.put_line('Inside FP');
     18          open c_fp;
     19          fetch c_fp into r_fp;
     20          update t23
     21          set person_name = upper(r_fp.person_name)
     22          where current of c_fp;
     23          close c_fp;
     24          commit;
     25      end foo_proc;
     26  begin
     27      for onerow in c_jd loop
     28          dbms_output.put_line('Outer loop START');
     29          foo_proc(onerow.person_name);
     30          dbms_output.put_line('Outer loop END');
     31      end loop;
     32  end;
     33  /
    Outer loop START
    Inside FP
    declare
    *
    ERROR at line 1:
    ORA-00060: deadlock detected while waiting for resource
    ORA-06512: at line 11
    ORA-06512: at line 18
    ORA-06512: at line 29
    
    
    SQL>
    

    (I added some DBMS_OUTPUT statements to show what’s happening).

    “When you said the code example I
    provided was bad practice, what do you
    mean?”

    I meant having a loop driving off a SELECT statement calling another program which selects from the same table. Indeed, which selects the very same row. Generally speaking, we should avoid doing unnecessary work. You already have the row: why read it again?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 447k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should try to avoid using the Count() method as… May 15, 2026 at 7:31 pm
  • Editorial Team
    Editorial Team added an answer The second parameter is the selector context. It restricts searching… May 15, 2026 at 7:31 pm
  • Editorial Team
    Editorial Team added an answer Your javascript is not actually calling anything. Rather, it is… May 15, 2026 at 7:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.