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 Problem with unlock method with wrong lockCookie 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 "Problem with unlock method with wrong lockCookie" Watch "Problem with unlock method with wrong lockCookie" New topic
Author

Problem with unlock method with wrong lockCookie

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
Quick question about an unlocking issue in the UrlyBird assignment 1.1.2.
When I run the following code, say for 1 of the methods, that uses a locking strategy, say update.

When I run the above with a different lockCookie as in the "123L" and I use the following strategy in my code for unlock:
(1) If cache contains record number for passed lockCookie Value, remove the cache entry and signal all the record number can be inserted.
or throw Exception.
My problem above is if 1 runs the above code for 2 or more clients, the first client has locked recNo, above is 20, but never releases it in the unlock method, because the latter method never got passed the correct lockCookie.
Just wondering how people approached this issue?
Thanks.
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

I would strongly suggest you stick to the the Specifications in the Sun interface for your assignment.

I'd think you received something like:



I do what my interface says.


HTH,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2264
    
    3

Howdy, Mark!

Mark O' Sullivan wrote:My problem above is if 1 runs the above code for 2 or more clients, the first client has locked recNo, above is 20, but never releases it in the unlock method, because the latter method never got passed the correct lockCookie.


Well, I'd say that you don't have to worry with this situation. The thing is, we have an API, and we have to use it correctly so that things happen correctly. When you call the lock() method, you receive a number that you will use to call the update() and unlock() methods. When calling these methods, the value you first received is expected to be passed to these methods. So, if you do everything correctly, no problems should occur. Another thing, you are considering that the record is locked forever, right... but in this case you would be using the API wrongly. You don't really have to create some sort of timeout mechanism; you would be over complicating things. Just expect the users to use the API correctly and everything will be fine!

Carlos Morillo wrote:I do what my interface says.


Alright! That's the spirit!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Hi Mark,

Like Roberto already mentioned: such a situation should never occur in your code, so you don't have to deal with it. If you encounter such a problem in your code, it means you have coded a bug and this situation should be discovered during testing.

But you should "protect" your Data class against wrong usage. For example: when someone calls the read-method before the init-method is called (this method initialises my record cache, calling read without the cache would be useless) I throw an IllegalStateException with a specific message. When you try to update a record without owning the lock on that record an IllegalStateException is also thrown with another message, clearly indicating you have to lock the record prior to updating. If you pass a null-value to my find-method an IllegalArgumentException is thrown. And so on... None of these exceptions are caught in my program, because if the Data class is used as described, these situations will never occur.
If you (or any other developer) have to make changes to my program (because I left the company) and he doesn't use the Data class as designed (and he didn't read the extensive javadoc comments) he will get a human-readible informative message about how that method should be used or what valid values are for a specific parameter.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with unlock method with wrong lockCookie