Hi Jan,
First and foremost, I should state that I believe you do not need to handle the cases where clients disconnect without releasing their locks. I have seen many candidates pass with good scores without worrying about this. (Disclaimer: I did handle this situation in my own submission).
Frans has a good suggestion for the case where client A already owns the lock. However allowing clients to lock more than one record can still result in deadlock. Searching on the
word deadlock should bring up some simple solutions, including
only allowing clients to lock records in numerical order (that is, client A cannot lock records 3, 5, and 4 in that order) - in which case deadlock detection can be done easily. Phil Maquet also described a recursive lock check which went further - it handled the cases where any number of clients could be locking any number of records in any order, and he could detect deadlocks.
Regarding a client disconnecting / crashing without releasing a lock (unlock() getting lost) - this is possibly one of the most discussed topics in this forum
.
Personally I don't like your suggestion of killing any locks that have been hanging around too long. The way I read the Sun assignments, once I have been given a lock, it is mine for as long as I want. There is nothing to suggest that it might be taken from me if I take too long. I
personally feel that implementing a timeout on locks is not implementing the provided interface - it is implementing a solution that is just easier for me to handle.
If you are using RMI, you could look at the Unreferenced interface - this will give you notification that a client has disconnected. If you are using Sockets you will receive an Exception when the client disconnects. In either case, you will have an opportunity to clean up orphaned locks.
Alternatively you can look at using a WeakHashMap to store your locks. When there is no longer a reference to the lock (the client has disconnected) the lock will be automatically removed from the WeakHashMap.
If you have questions about any of these, please ask them. Otherwise, possible keywords in this forum are: orphan, WeakHashMap, Unreferenced.
Regards, Andrew