The derby locking system implements livelock avoidance as you describe.
So if one or more xacts have a shared lock and then an exclusive
lock queues waiting for the resource, subsequent share locks will
wait behing the exclusive lock. The only exception to this is the
case of compatible "release-immediate" locks, sometimes during
read committed scans we will request a shared lock and tell the lock
manager to logically grant and release it in one step. In these
cases it will not queue behind the exclusive lock since it won't
get in its way.
Bryan Pendleton wrote:
When a thread tries to create a table, it will first get a shared lock
on the dictionary (DataDictionaryImpl.startReading). This is released
before it tries to lock the dictionary exclusively. The way
DataDictionaryImpl.startwriting works is that it first checks whether
someone is holding a lock on the dictionary. If so, it will sleep for
a while a then try again. This goes on for a while until it gets
impatient and actually requests an exclusive lock and enters the lock
queue. In the mean time, a lot more threads have acquired a shared
lock and the updating thread will have to wait for all of them to
release it.
This sounds like there is a lock scheduling fairness issue.
Your description makes it sounds like this:
- One or more transactions get shared locks on a resource
- A transaction requests an exclusive lock on a resource, and blocks
- Additional transactions arrive, requesting shared locks, and their
locks are granted.
It is possible to implement a lock scheduling policy such that those
later locks, even though they are compatible with all currently granted
locks, are not immediately granted, but instead are placed onto the
resource queue behind the pending exclusive request.
Perhaps we should consider whether there is an opportunity to enhance
the locking policies to be more fair to writers?
thanks,
bryan