oracle-databaserowlocking

Which of the several sessions waiting to acquire an exclusive row lock will be notified first upon release in Oracle RDBMS?


Suppose the following:

  1. There is a user session which holds exclusive locks on several rows in the table.
  2. Two (or more) other sessions are started, each with the request to acquire some of the currently held locks
    • To make the case easier, let's assume that the new sessions want to acquire just one row lock, and they both want the same one. In reality it's unlikely to happen exactly that way, because each session will try to lock an "island" of connected rows, but I doubt this impacts the answer to this question.
  3. Documentation on concurrency says, that:

    If the lock is active, then the session asks to be notified when the lock is released.`

  4. The original session which held the locks commits and releases its locks.

Which one of the two waiting sessions will get notified that it is now able to acquire locks? Is there a DB setting that controls this?

If the waiters are notified in random order, does that mean that there is a possibility of lock-starving one of the sessions when a lot of them compete for the same lock, or is there a mechanism to address this case?


Solution

  • When you're after a resource (and someone else owns it) you go on a "waiters" queue, and as the name suggests, you get "in line".

    If you really want to dig into the weeds, have one session lock a row, then get a couple of others to wait for that row, and then do:

    alter session set events = 'immediate trace name enqueues level 3';
    

    to get a trace file showing the various queues floating around.

    An excellent resource for this is: Oracle Core, by Jonathan Lewis.