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.3.2: Where do you call the Data class's lock() and unlock() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird 1.3.2: Where do you call the Data class Watch "URLyBird 1.3.2: Where do you call the Data class New topic
Author

URLyBird 1.3.2: Where do you call the Data class's lock() and unlock() method?

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
After I read some topic according to lock mechanism, I find that most rachers use the lock() and unlock() like this:

or
...
lock(recNo);
update(recNo,data);
unlock(recNo);
...


And it looks like these codes are all in the client side.

Did anyone use the lock() and unlock() method like this:



SCJP,SCWCD1.3,SCWCD1.4,SCJD,SCBCD5,SCEA5
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11478
    
  94

Hi Sun,

This may not be a good idea. I will leave the argument over Should lock methods be callable by the client alone for now, as that is separate from the potential problem you might cause yourself.

The problem I see is that calling the lock method from within the update method leaves the potential for somebody to overwrite another person's booking (or even book a deleted record). Consider the following:
  • Client A checks that record 1 has not been booked
  • Client B checks that record 1 has not been booked
  • Client A books record 1 (calls the update() method)
  • Client B books record 1 (calls the update() method)
  • Now client A thinks that they have successfully booked record 1, but the database shows that client B owns the record

    This problem can be overcome by using the lock() method to logically block one client from even confirming availability the record until the other client has finished.

    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.3.2: Where do you call the Data class's lock() and unlock() method?