Win a copy of Android Programming: The Big Nerd Ranch Guide this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Mellon Sun
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12105
246
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!