Oracle user locks (dbms_lock): guaranteed order of requests granted

lockingoracle

I was wondering if Oracle guarantees that the sequence of dbms_lock.request granted is the same as the sequence in which they were requested.
In my tests it always was the case, but I couldn't find any official statement in the Oracle documentation.

So let's assume we have at least three sessions:

  1. Session 1 requests the lock via dbms_lock.request(1) and is granted the lock immediately (as the lock is not yet held by any other session)
  2. Session 2 requests the lock via dbms_lock.request(1) and has to wait
  3. Session 3 requests the lock via dbms_lock.request(1) and also has to wait
  4. Now Session 1 releases the lock via dbms_lock.release(1) (or trx end or session end)

Is it guaranteed that Session 2 will now receive the lock and Session 3 still has to wait until Session 2 releases? Or might Session 3 receive the lock before Session 2?

Edit: if the answer is "yes, guaranteed", please provide link to Oracle documentation, whitepaper, V$ view or the like.

Edit 2: Database version in question is 11g and up

Best Answer

Yes!

There is a wait queue and the first requester is the first served. Beware of deadlocks...