• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLyBird 1.1.3 Locking algorithm

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic