permaculture playing cards
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Record-level locking question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Record-level locking question" Watch "Record-level locking question" New topic

Record-level locking question

Jimmy Ho
Ranch Hand

Joined: Jul 31, 2007
Posts: 61
Sorry for the numerous questions. Internet access is sparse here in Iraq, so my posts tend to be "batch-posted" when I get online.

I'm implementing database record-locking. I have a Map<Long, Long> where key=recNo, and value=cookie. If there's an entry in the Map for that recNo, then the record is locked. When I use the wait() and notifyAll() methods, I'm using them on the map itself. I don't see how I can do it elegantly on a record-by-record basis.

My assignment specifies that if a client needs to lock a record, and the record is already locked, then the thread needs to go to sleep. In fact, it states that the thread must be "consuming no CPU cycles until the desired resource becomes available". By locking on the Map and not per record, I may be violating this. However it says that it "should", and not "must", so I guess I'm sort of safe?

Is there a way to wait() on a record instead of on the whole Map?

Obviously, I can set up something to do this, but every solution I come up with seems overkill and kludgey (like maintaining a HashSet or array of arbitrary objects to lock on). Locking on the Long objects seems a little flimsy. I like my simple Map<Long, Long>. Any ideas?
Alexander Duenisch

Joined: May 29, 2006
Posts: 24
From my understanding, there is no need to wait() on an individual record.
You can just do it like this:

This works because the wait() is in a while loop. If a thread encounters an already locked record, it goes into waiting state, releasing the lock. If another thread unlocks a record, all waiting threads get woken up. Now, each one of them checks if it was the record they are interested in, that was unlocked. If a thread was waiting on the unlocked record, then it can proceed with the locking business, otherwise it goes to sleep again. If I'm not completely mistaken, there should be no potential deadlocks here, as there is a corresponding call to unlock() for each locked record.
What do you think?

[ April 19, 2008: Message edited by: Alexander D´┐Żnisch ]

---------------------------<br />OCUP-F, SCJP 5.0, SCJD, SCWCD
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I have also taken into account the possibility that the current thread is actually the owner of the record lock, just in case there are nested lock requests. In this case, I do not make the thread wait, because it already owns the lock.
Alexander Duenisch

Joined: May 29, 2006
Posts: 24
Sure, it's the same with me. The wait() loop is actually nested in an if-clause, that checks for lock ownership. If the current thread owns the lock, then the wait() will be skipped. I just didn't post the full code for the sake of simplicity.
I agree. Here's the link:
subject: Record-level locking question
jQuery in Action, 3rd edition