File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about lock/unlock sequence

 
Kevin Cao
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen lots of people have method like this:
My question is what if the code is broken after data.lock()? So you don't have a chance to unlock the record.
If we modify this method to one of the two ways:

or

To make sure the lock-read-modify-unlock sequence is correct.
Your comments are appreciated!
Thanks,
Kevin
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first modification doesn't do anything. (I certainly hope you don't plan to implement this method on the server!!!) The second is great -- very perceptive, you identified a genuine issue that you need to address.
- Peter
 
Kevin Cao
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, Peter.
The first one really looks wired, all I want to do is make the method atomic, so the lock-read-modify-unlock will not be broken. I am new to thread, I couldn't convince myself why it is not right even nested synchrinization is never a good thing.
-Kevin
 
BJ Grau
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin -
Some more food for thought - take your example one step further into the locking mechanism itself and think about what happens if a client dies after locking a record and finally and therefore unlock never gets called.
-BJ
[ February 21, 2003: Message edited by: BJ Grau ]
 
Kevin Cao
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BJ-
As I understand, the only time finally statement would not be executed is if the System.exit()method terminates the program. So, data.unlock() will execute no matter what.
-Kevin
 
BJ Grau
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kevin Cao:
BJ-
As I understand, the only time finally statement would not be executed is if the System.exit()method terminates the program. So, data.unlock() will execute no matter what.
-Kevin

You are right, what I meant by a client dying is the network connection to the server goes away. In general, my point is what if unlock fails for some reason? For example, you have exposed the methods of Data to the client through some remote object. You have a client business class with a method such as bookFlight that calls lock read modify unlock against the remote object that exposes Data to the client. Suddenly, the client's network connection fails, and the call to unlock on the remote Data never makes it to the server. Now you have a record locked and the owner is gone.
[ February 21, 2003: Message edited by: BJ Grau ]
 
Kevin Cao
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BJ-
Thanks for our response. I think the argument here is whether this method an atomic operation, or whether lock-read-modify-unlock will be broken. I can careless if unlock fails.
-Kevin
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic