This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question about lock/unlock sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question about lock/unlock sequence" Watch "Question about lock/unlock sequence" New topic
Author

Question about lock/unlock sequence

Kevin Cao
Ranch Hand

Joined: Jun 19, 2002
Posts: 55
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jun 19, 2002
Posts: 55
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

Joined: Jul 10, 2001
Posts: 234
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

Joined: Jun 19, 2002
Posts: 55
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

Joined: Jul 10, 2001
Posts: 234
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

Joined: Jun 19, 2002
Posts: 55
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.
 
subject: Question about lock/unlock sequence
 
Similar Threads
lock-modify- unlock
NX: Client side lock vs Server side lock
Complete locking schema or how to avoid 44/80 on locking.
Question about Unreferenced
Locking & Threading Issues...HELP!