Originally posted by Yanxin Zhao:
Hi everyone:
I got a question on data locking. In network mode, if a client locked a record, and when it wants to update this record, this client shutdown, but it does not give up the lock. at the same time, another client wants to update this record too, it will check that whether this record is locked or not, so the answer is "locked". so, from now on, no client can updates this record.
So, I want to ask how to deal with this problem in server side? I think that why someone got 80/80, and someone got 44/80 in locking aspect.
Dealing with orphan locks is not a requirement of the assignment. People who have both dealt with them and didn't have received 44/80 and 80/80. If it were a requirement, ignoring it would get an automatic failure.
All that said, its actually quite simple to handle these. You must detect the loss of a client and then unlock any locks held by that client. Detecting the client disconnect or failure requres some kind of keepalive. If you are using RMI you can use the Unreferenced interface and java.rmi.dgc.leaseValue property to do this. If you are using sockets you can use the setSoTimeout method to detect clients that have not sent a command in specific time.
For either of these to work, each client must have its own instance Data on the server. Here's what I do with RMI:
- server startup sets java.rmi.dgc.leaseValue and sun.rmi.dgc.checkInterval
- the server registers a DataAdapterFactory with the rmi registry
- a client gets a reference to the DataAdapter from the DataAdapterFactory
- the DataAdapterImpl on the server implements Unreferenced
- if the DataAdapterImpl goes unreferenced it closes the Data object, which requests that the LockManager unlock any records locked by that Data object.