File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird 1.1.3  Locking algorithm 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 "URLyBird 1.1.3  Locking algorithm" Watch "URLyBird 1.1.3  Locking algorithm" New topic
Author

URLyBird 1.1.3 Locking algorithm

Mark Waldrop
Greenhorn

Joined: Jan 04, 2004
Posts: 9
Does the following algorithm sound good for the Lock and Unlock?

long Lock(int):

Initialization
1. During the Data base interface construction, create a Random class with an input of the current Time (in ms).
2. Initialize hash map to hold locked records

Lock attempting to be performed
0. Check and make sure that the record exists
0.5 If it does not, throw a RecordNotFoundException()
1. First check that record is not already locked (check in the hash map)
2. If the record is already locked, then perform a wait()
3. If it is not, then get the next Random long sequence and insert that with the record #, into the hash map.
4. Return the Random long sequence as the lockCookie

void unlock( int, long):
1. Make sure that the record is indeed locked and it exists
1.5 If record does not exist, throw a RecordNotFoundException()
2. If it is, then get the value associated to the key and compare it to the
lockCookie passed into this method.
3. If they match, then (remove the key, value pair from the hash map) or (set the key's value to null) (haven't figured out which one is better yet) and return
4. Else if they do not match, throw a SecurityException


Please let me know what you think or ways to tweak this.

Also, should we somehow add a time limit to the locked records, in the case that one of the client sessions went down after they locked a record. Now that client will not have the lockCookie any longer.

Thanks,
Mark Waldrop
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Mark Waldrop:
Does the following algorithm sound good for the Lock and Unlock?

long Lock(int):

Initialization
1. During the Data base interface construction, create a Random class with an input of the current Time (in ms).
2. Initialize hash map to hold locked records

Lock attempting to be performed
0. Check and make sure that the record exists
0.5 If it does not, throw a RecordNotFoundException()
1. First check that record is not already locked (check in the hash map)
2. If the record is already locked, then perform a wait()
3. If it is not, then get the next Random long sequence and insert that with the record #, into the hash map.
4. Return the Random long sequence as the lockCookie

void unlock( int, long):
1. Make sure that the record is indeed locked and it exists
1.5 If record does not exist, throw a RecordNotFoundException()
2. If it is, then get the value associated to the key and compare it to the
lockCookie passed into this method.
3. If they match, then (remove the key, value pair from the hash map) or (set the key's value to null) (haven't figured out which one is better yet) and return
4. Else if they do not match, throw a SecurityException


Please let me know what you think or ways to tweak this.

Also, should we somehow add a time limit to the locked records, in the case that one of the client sessions went down after they locked a record. Now that client will not have the lockCookie any longer.

Thanks,
Mark Waldrop


Timeouts on locks are not the way to go, if you do you will need to change the DBAccess API to provide for the case of a lock timing out. A much better solution is to let the network code deal with this by closing the Data object associated with a client or business operation when a network failure occurs. Closing that should unlock any associated locked records. This is very easy to do in a Socket based network and a bit more complex in RMI. See my description of the RMI solution for more details.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Hi Mark,

You have not mentioned notifying clients that a lock is available when releasing a lock.

I agree with Peter that lock timeouts require a change to the specification, which I think is wrong. Aside from Peter's solution, you can also use java.rmi.server.Unreferenced to get notified whenever a client disconnects (deliberately or because of a crash or network errors), which gives you the opportunity to clean up the locks.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird 1.1.3 Locking algorithm